<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    JAVA

    人生若只如初見,何事秋風悲畫扇。

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      50 隨筆 :: 25 文章 :: 157 評論 :: 0 Trackbacks

     PO即 Persistence Object
      VO即 Value Object

     VO和PO的主要區(qū)別在于:
      VO是獨立的Java Object。
      PO是由Hibernate納入其實體容器(Entity Map)的對象,它代表了與數(shù)據(jù)庫中某條記錄對應的Hibernate實體,PO的變化在事務提交時將反應到實際數(shù)據(jù)庫中。

     實際上,這個VO被用作Data Transfer Object,即所謂的DTO。想必,VO就是Data Access Object ---DAO了啦。為什么要有這二者之分呢?如在傳統(tǒng)的MVC架構(gòu)中,位于Model層的PO,是否允許被傳遞到其他層面。由于PO的更新最終將被映射到實際數(shù)據(jù)庫中,如果PO在其他層面(如View層)發(fā)生了變動,那么可能會對Model層造成意想不到的破壞。

     主要想說的還是如何進行二者之間的轉(zhuǎn)換:
      屬性復制可以通過Apache Jakarta Commons Beanutils(http://jakarta.apache.org/commons/beanutils/)組件提供的屬性批量復制功能,避免繁復的get/set操作。down下來之后,里面的API DOC一應俱全。

     對于一些無需處理其它處理(如過濾)直接用BeanUtilsBean.copyProperties方法,其參考如下:

    public static void copyProperties(java.lang.Object dest,
    ????????????????????????????????? java.lang.Object orig)
    ?????????????????????????? throws java.lang.IllegalAccessException,
    ????????????????????????????????? java.lang.reflect.InvocationTargetExceptioCopy property values from the origin bean to the destination bean for all cases where the property names are the same.


    ?
      范例1:

    TUser?user? = ? new ?TUser();
    TUser?anotherUser?
    = ? new ?TUser();
    user.setName(
    " Emma " );
    user.setUserType(
    1 );
    try ? {
    BeanUtils.copyProperties(anotherUser,user);
    System.out.println(
    " UserName?=>? "
    + anotherUser.getName()
    );
    System.out.println(
    " UserType?=>? "
    + ?anotherUser.getUserType()
    );
    }
    ? catch ?(IllegalAccessException?e)? {
    e.printStackTrace();
    }
    ? catch ?(InvocationTargetException?e)? {
    e.printStackTrace();
    }
     



     也可以利用其中的一些方法在copy屬性的時候達到自己的要求,如:

     范例2

    /** //*
    ?*?Created?on?2006-4-26
    ?
    */

    package ?com.util;

    import ?java.beans.PropertyDescriptor;
    import ?java.util.Collection;

    import ?org.apache.commons.beanutils.PropertyUtils;


    /** ? */ /**
    ?*?CopyUtil
    ?*?
    @author ?Jkallen
    ?
    */

    public ? class ?CopyUtil? {
    ????
    ????
    /** ? */ /**
    ?????*?Copy?properties?of?orig?to?dest
    ?????*?Exception?the?Entity?and?Collection?Type
    ?????*?
    @param ?dest
    ?????*?
    @param ?orig
    ?????*?
    @return ?the?dest?bean
    ?????
    */

    ????
    public ? static ?Object?copyProperties(Object?dest,?Object?orig)? {
    ????????
    if ?(dest? == ? null ? || ?orig? == ? null )? {
    ????????????
    return ?dest;
    ????????}

    ????????
    ????????PropertyDescriptor[]?destDesc?
    = ?PropertyUtils.getPropertyDescriptors(dest);
    ????????
    try ? {
    ????????????
    for ?( int ?i? = ? 0 ;?i? < ?destDesc.length;?i ++ )? {
    ????????????????Class?destType?
    = ?destDesc[i].getPropertyType();
    ????????????????Class?origType?
    = ?PropertyUtils.getPropertyType(orig,?destDesc[i].getName());
    ????????????????
    if (destType? != ? null ? && ?destType.equals(origType)
    ????????????????????????
    && ? ! destType.equals(Class. class ))? {
    ????????????????????
    if ( ! Collection. class .isAssignableFrom(origType)) {????????????????????
    ????????????????????????
    try {
    ????????????????????????????Object?value?
    = ?PropertyUtils.getProperty(orig,?destDesc[i].getName());
    ????????????????????????????PropertyUtils.setProperty(dest,?destDesc[i].getName(),?value);
    ????????????????????????}
    catch (Exception?ex) {????????????????????????????
    ????????????????????????}

    ????????????????????}

    ????????????????}

    ????????????}

    ????????????
    ????????????
    return ?dest;
    ????????}
    catch (Exception?ex)? {
    ????????????
    throw ? new ?CopyException(ex);
    // ????????????return?dest;
    ????????}

    ????}
    ????
    ????
    ????
    /** ? */ /**
    ?????*?Copy?properties?of?orig?to?dest
    ?????*?Exception?the?Entity?and?Collection?Type
    ?????*?
    @param ?dest
    ?????*?
    @param ?orig
    ?????*?
    @param ?ignores
    ?????*?
    @return ?the?dest?bean
    ?????
    */

    ????
    public ? static ?Object?copyProperties(Object?dest,?Object?orig,?String[]?ignores)? {
    ????????
    if ?(dest? == ? null ? || ?orig? == ? null )? {
    ????????????
    return ?dest;
    ????????}

    ????????
    ????????PropertyDescriptor[]?destDesc?
    = ?PropertyUtils.getPropertyDescriptors(dest);
    ????????
    try ? {
    ????????????
    for ?( int ?i? = ? 0 ;?i? < ?destDesc.length;?i ++ )? {
    ????????????????
    if ?(contains(ignores,?destDesc[i].getName()))? {
    ????????????????????
    continue ;
    ????????????????}

    ????????????????
    ????????????????Class?destType?
    = ?destDesc[i].getPropertyType();
    ????????????????Class?origType?
    = ?PropertyUtils.getPropertyType(orig,?destDesc[i].getName());
    ????????????????
    if (destType? != ? null ? && ?destType.equals(origType)
    ????????????????????????
    && ? ! destType.equals(Class. class ))? {
    ????????????????????
    if ( ! Collection. class .isAssignableFrom(origType)) {
    ????????????????????????Object?value?
    = ?PropertyUtils.getProperty(orig,?destDesc[i].getName());
    ????????????????????????PropertyUtils.setProperty(dest,?destDesc[i].getName(),?value);
    ????????????????????}

    ????????????????}

    ????????????}

    ????????????
    ????????????
    return ?dest;
    ????????}
    catch (Exception?ex)? {
    ????????????
    throw ? new ?CopyException(ex);
    ????????}

    ????}

    ????
    ????
    static ? boolean ?contains(String[]?ignores,?String?name)? {
    ????????
    boolean ?ignored? = ? false ;
    ????????
    for ?( int ?j? = ? 0 ;?ignores? != ? null ? && ?j? < ?ignores.length;?j ++ )? {
    ????????????
    if ?(ignores[j].equals(name))? {
    ????????????????ignored?
    = ? true ;
    ????????????????
    break ;
    ????????????}

    ????????}

    ????????
    ????????
    return ?ignored;
    ????}

    }


      

      可以看到,在范例1中通過方法copyProperties的時候,二者之間在的屬性名必須相同(Copy property values from the origin bean to the destination bean for all cases where the property names are the same)。而在范例2中通過
    ?  Object value = PropertyUtils.getProperty(orig, destDesc[i].getName());
    ?   PropertyUtils.setProperty(dest, destDesc[i].getName(), value);
      也是將源與目的之間copy相同的屬性名。而VO是在前臺顯示,所以難免會用到PO中所不存在的屬性值。比如PO中可能是一個對象,而VO中則可能是此對象的全部屬性。其中的一些轉(zhuǎn)換則需要依據(jù)前臺需?要針對性地處理啦!
    ?????????在copy的過程中,若實體中存在一對多,多對多等關系,則DTO中也應該存在此關系,此時不能直接將內(nèi)部的DTO or List(一對多)里面的數(shù)據(jù)一次性全部拷過去,這時應該對每個DTO進行copy,當然,若在前臺你無需用到相關的DTO則可以跳過。而對于一對多(LIST),而應將實體里面的每個一對多轉(zhuǎn)換成對應的DTO,再依次放到LSIT里面,再將此LIST賦值給(父)DTO,這里面的關系如同(一個簡單的)遞歸關系。---更新于5-25-2006

    Reference:? Apache DOC and <<Hibernate 開發(fā)指南>>???????????????

    posted on 2006-04-26 14:55 Jkallen 閱讀(6690) 評論(8)  編輯  收藏 所屬分類: JEE學習 、其它開源

    評論

    # re: VO(DTO)與PO(DAO)之間的轉(zhuǎn)換 2006-04-26 17:48 寒晴天
    我想問你你代碼的樣式怎么弄的啊.好看,真好看.  回復  更多評論
      

    # re: VO(DTO)與PO(DAO)之間的轉(zhuǎn)換 2006-04-26 17:48 寒晴天
    說話,說話.  回復  更多評論
      

    # re: VO(DTO)與PO(DAO)之間的轉(zhuǎn)換 2006-04-26 17:50 寒晴天
    看的不是太明白哦  回復  更多評論
      

    # re: VO(DTO)與PO(DAO)之間的轉(zhuǎn)換 2006-04-27 08:51 藍天
    我倒!
    這個兄弟怎么不抓住重點呢?
      回復  更多評論
      

    # re: VO(DTO)與PO(DAO)之間的轉(zhuǎn)換 2006-04-27 08:54 藍天
    編輯的時候注意下,也有的.  回復  更多評論
      

    # re: VO(DTO)與PO(DAO)之間的轉(zhuǎn)換 2006-04-29 12:53 dennis
    試試dozer吧,更為強大  回復  更多評論
      

    # re: VO(DTO)與PO(DAO)之間的轉(zhuǎn)換[未登錄] 2007-05-08 01:09 jerry
    首先謝謝你,我還想問你要點一對多的代碼可以嗎/
    yangjiehuan@163.com  回復  更多評論
      

    # re: VO(DTO)與PO(DAO)之間的轉(zhuǎn)換 2007-05-10 19:24 藍天
    一年前用DTO,DAO來配合JSP顯示數(shù)據(jù),
    現(xiàn)在看來,這是不可取的: 增加了很多的代碼量 & 增加了系統(tǒng)中的文件 & 日后維護麻煩(地方太多,顯得過于雜亂)

    solve: 在spring中通過bind可以捆定相關的對象. 結(jié)合hibernate 可以很好解決以上遇到的問題

      回復  更多評論
      

    主站蜘蛛池模板: 免费黄网站在线看| 好看的电影网站亚洲一区| 免费无码中文字幕A级毛片| 久久亚洲中文无码咪咪爱| 亚洲经典在线中文字幕| 亚洲精品国产精品乱码在线观看 | 狠狠色婷婷狠狠狠亚洲综合| 色窝窝免费一区二区三区 | 亚洲AV无码精品色午夜果冻不卡 | 亚洲第一福利网站在线观看| 免费无码AV片在线观看软件| 1000部拍拍拍18勿入免费视频下载| 中文成人久久久久影院免费观看| 国产亚洲蜜芽精品久久| 亚洲综合色区中文字幕| 亚洲精品中文字幕无码AV| 亚洲AV永久青草无码精品| 亚洲宅男天堂在线观看无病毒| 免费在线看片网站| 国产无遮挡吃胸膜奶免费看 | 亚洲av日韩av无码黑人| 亚洲桃色AV无码| 久久精品国产精品亚洲| 一级毛片直播亚洲| 国产一区二区视频免费| 国产高清免费观看| 免费高清在线影片一区| 成年在线网站免费观看无广告| 青青青免费国产在线视频小草| 亚洲一级免费毛片| 免费观看美女用震蛋喷水的视频| 亚洲视频免费在线播放| 亚洲一区二区三区免费在线观看| 69pao强力打造免费高清| 亚洲一区免费视频| 猫咪社区免费资源在线观看| 在线视频观看免费视频18| 毛片a级毛片免费播放下载| 女人18毛片a级毛片免费视频| 91免费精品国自产拍在线不卡| 国产成人午夜精品免费视频|