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

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

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

    浪跡天涯
    web報表設(shè)計器....
    posts - 61,comments - 71,trackbacks - 0

    PO即 Persistence Object
      VO即 Value Object

     VO和PO的主要區(qū)別在于:
      VO是獨立的Java Object。
      PO是由Hibernate納入其實體容器(Entity Map)的對象,它代表了與數(shù)據(jù)庫中某條記錄對應(yīng)的Hibernate實體,PO的變化在事務(wù)提交時將反應(yīng)到實際數(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)換:
      屬性復(fù)制可以通過Apache Jakarta Commons Beanutils(http://jakarta.apache.org/commons/beanutils/)組件提供的屬性批量復(fù)制功能,避免繁復(fù)的get/set操作。down下來之后,里面的API DOC一應(yīng)俱全。

     對于一些無需處理其它處理(如過濾)直接用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ù)前臺需要針對性地處理啦!

    Reference:? Apache DOC and <>

    posted on 2006-06-08 15:49 JJCEA 閱讀(674) 評論(0)  編輯  收藏 所屬分類: 學(xué)習日記
    主站蜘蛛池模板: 亚洲黄片手机免费观看| 日韩色视频一区二区三区亚洲 | 亚洲伊人久久大香线蕉综合图片 | 亚洲熟妇无码八V在线播放| 亚洲va无码va在线va天堂| 免费大片黄手机在线观看| 成人免费一区二区无码视频| 午夜不卡久久精品无码免费| 久久www免费人成看国产片| 亚洲av无码有乱码在线观看| 亚洲av乱码一区二区三区香蕉| 国产AV无码专区亚洲AV男同 | 中文字幕版免费电影网站| 亚洲aⅴ无码专区在线观看春色 | 少妇无码一区二区三区免费| 中国极品美軳免费观看| 夜夜爽妓女8888视频免费观看| 亚洲乱码av中文一区二区| 亚洲一区在线观看视频| 久久综合亚洲色HEZYO社区| 皇色在线免费视频| 香港一级毛片免费看| 亚洲国产午夜精品理论片在线播放 | 亚洲人成网站日本片| 亚洲高清无在码在线电影不卡| 亚洲综合精品香蕉久久网| 在线观看亚洲av每日更新| 亚洲中文字幕无码一区| 国产亚洲av片在线观看18女人| 亚洲AV成人精品日韩一区18p| 亚洲人成无码网站在线观看 | 在线观看免费无码视频| 免费播放美女一级毛片| jizzjizz亚洲日本少妇| 久久综合久久综合亚洲| 亚洲熟妇无码八V在线播放| 亚洲看片无码在线视频| 亚洲狠狠婷婷综合久久| 色天使亚洲综合一区二区| 野花视频在线官网免费1| 一级美国片免费看|