亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

PO 轉(zhuǎn) VO 復(fù)制

系統(tǒng) 1994 0
  1. import ??java.beans.PropertyDescriptor; ??
  2. import ??java.util.Collection; ??
  3. ??
  4. import ??org.apache.commons.beanutils.PropertyUtils; ??
  5. ??
  6. ??
  7. /**? ?
  8. *?CopyUtil ?
  9. ?*/ ? ??
  10. ? public ??? class ??CopyUtil???{ ??
  11. ??? ??
  12. ???? /**? ?
  13. ????*?Copy?properties?of?orig?to?dest ?
  14. ????*?Exception?the?Entity?and?Collection?Type ?
  15. ????*??@param??dest ?
  16. ????*??@param??orig ?
  17. ????*??@return??the?dest?bean ?
  18. ?????*/ ? ??
  19. ????? public ??? static ??Object?copyProperties(Object?dest,?Object?orig)???{ ??
  20. ???????? if ??(dest??==??? null ???||??orig??==??? null ?)???{ ??
  21. ???????????? return ??dest; ??
  22. ???????}? ??
  23. ??????? ??
  24. ???????PropertyDescriptor[]?destDesc??=??PropertyUtils.getPropertyDescriptors(dest); ??
  25. ???????? try ????{ ??
  26. ???????????? for ??(? int ??i??=??? 0 ?;?i??<??destDesc.length;?i?++?)???{ ??
  27. ???????????????Class?destType??=??destDesc[i].getPropertyType(); ??
  28. ???????????????Class?origType??=??PropertyUtils.getPropertyType(orig,?destDesc[i].getName()); ??
  29. ???????????????? if ?(destType??!=??? null ???&&??destType.equals(origType) ??
  30. ????????????????????????&&???!?destType.equals(Class.? class ?))???{ ??
  31. ???????????????????? if ?(?!?Collection.? class ?.isAssignableFrom(origType))??{???????????????????? ??
  32. ???????????????????????? try ??{ ??
  33. ???????????????????????????Object?value??=??PropertyUtils.getProperty(orig,?destDesc[i].getName()); ??
  34. ???????????????????????????PropertyUtils.setProperty(dest,?destDesc[i].getName(),?value); ??
  35. ???????????????????????}? catch ?(Exception?ex)??{???????????????????????????? ??
  36. ???????????????????????}? ??
  37. ???????????????????}? ??
  38. ???????????????}? ??
  39. ???????????}? ??
  40. ??????????? ??
  41. ???????????? return ??dest; ??
  42. ???????}? catch ?(Exception?ex)???{ ??
  43. ???????????? throw ??? new ??CopyException(ex); ??
  44. //?????????????return?dest;? ??
  45. ????????}? ??
  46. ???}????? ??
  47. ??? ??
  48. ???? /**? ?
  49. ????*?Copy?properties?of?orig?to?dest ?
  50. ????*?Exception?the?Entity?and?Collection?Type ?
  51. ????*??@param??dest ?
  52. ????*??@param??orig ?
  53. ????*??@param??ignores?例如:vo.setUserName?copy?po.setUserName,應(yīng)該寫UserName ?
  54. ????*??@return??the?dest?bean ?
  55. ?????*/ ? ??
  56. ????? public ??? static ??Object?copyProperties(Object?dest,?Object?orig,?String[]?ignores)???{ ??
  57. ???????? if ??(dest??==??? null ???||??orig??==??? null ?)???{ ??
  58. ???????????? return ??dest; ??
  59. ???????}? ??
  60. ??????? ??
  61. ???????PropertyDescriptor[]?destDesc??=??PropertyUtils.getPropertyDescriptors(dest); ??
  62. ???????? try ????{ ??
  63. ???????????? for ??(? int ??i??=??? 0 ?;?i??<??destDesc.length;?i?++?)???{ ??
  64. ???????????????? if ??(contains(ignores,?destDesc[i].getName()))???{ ??
  65. ???????????????????? continue ?; ??
  66. ???????????????}? ??
  67. ??????????????? ??
  68. ???????????????Class?destType??=??destDesc[i].getPropertyType(); ??
  69. ???????????????Class?origType??=??PropertyUtils.getPropertyType(orig,?destDesc[i].getName()); ??
  70. ???????????????? if ?(destType??!=??? null ???&&??destType.equals(origType) ??
  71. ????????????????????????&&???!?destType.equals(Class.? class ?))???{ ??
  72. ???????????????????? if ?(?!?Collection.? class ?.isAssignableFrom(origType))??{ ??
  73. ???????????????????????Object?value??=??PropertyUtils.getProperty(orig,?destDesc[i].getName()); ??
  74. ???????????????????????PropertyUtils.setProperty(dest,?destDesc[i].getName(),?value); ??
  75. ???????????????????}? ??
  76. ???????????????}? ??
  77. ???????????}? ??
  78. ??????????? ??
  79. ???????????? return ??dest; ??
  80. ???????}? catch ?(Exception?ex)???{ ??
  81. ???????????? throw ??? new ??CopyException(ex); ??
  82. ???????}? ??
  83. ???}? ??
  84. ??? ??
  85. ???? static ??? boolean ??contains(String[]?ignores,?String?name)???{ ??
  86. ???????? boolean ??ignored??=??? false ?; ??
  87. ???????? for ??(? int ??j??=??? 0 ?;?ignores??!=??? null ???&&??j??<??ignores.length;?j?++?)???{ ??
  88. ???????????? if ??(ignores[j].equals(name))???{ ??
  89. ???????????????ignored??=??? true ?; ??
  90. ???????????????? break ?; ??
  91. ???????????}? ??
  92. ???????}? ??
  93. ??????? ??
  94. ???????? return ??ignored; ??
  95. ???}? ??
  96. ???
     
 import  java.beans.PropertyDescriptor;
 import  java.util.Collection;

 import  org.apache.commons.beanutils.PropertyUtils;


 /** 
 * CopyUtil
  */ 
  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 例如:vo.setUserName copy po.setUserName,應(yīng)該寫UserName
     *  @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;
    } 
} 
  




Java代碼 復(fù)制代碼
  1. public ? class ?PO2VO? extends ?TestCase?{ ??
  2. ??
  3. ???? /*?(non-Javadoc) ?
  4. ?????*?@see?junit.framework.TestCase#setUp() ?
  5. ?????*/ ??
  6. ???? protected ? void ?setUp()? throws ?Exception?{ ??
  7. ???????? super .setUp(); ??
  8. ????} ??
  9. ???? ??
  10. ???? public ? void ?testPO2VO(){ ??
  11. ????????TUserBasicVO?vo?=? new ?TUserBasicVO(); ??
  12. ????????TUserBasic?po?=? new ?TUserBasic(); ??
  13. ????????po.setPwd( "111" ); ??
  14. ????????po.setUserName( "222" ); ??
  15. ????????String[]?a={ "Pwd" , "UserName" }; ??
  16. ???????? try ?{ ??
  17. ????????????CopyUtil.copyProperties(vo,?po,a); ??
  18. ????????}? catch ?(Exception?e)?{ ??
  19. ???????????? //?TODO?Auto-generated?catch?block ??
  20. ????????????e.printStackTrace(); ??
  21. ????????} ??
  22. ????????System.out.println(vo.getPwd()); ??
  23. ????} ??
  24. }??

PO 轉(zhuǎn) VO 復(fù)制


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 97成人资源 | 综合色久七七综合七七蜜芽 | 老子影院午夜久久亚洲 | 亚洲一区二区三区四区五区 | 狠色狠狠色狠狠狠色综合久久 | 涩涩www在线观看免费高清 | 日本中文字幕有码 | 国内精品免费久久影院 | a毛片免费观看 | 天天综合天天综合色在线 | 亚洲在线视频播放 | 欧美一区二区三区在线播放 | 欧美成人看片一区二区三区 | 伊人色综合网一区二区三区 | 国语精品91自产拍在线观看二区 | 免费一区二区三区免费视频 | 亚洲综合亚洲综合网成人 | 精品久久久99大香线蕉 | 手机看片高清国产日韩片 | 久久伊人一区二区三区四区 | 日韩精品无码一区二区三区 | 真实国产乱子伦精品免费 | 精品一区二区三区亚洲 | 美女美女高清毛片视频 | 福利在线播放 | 乱在线伦视频免费 | 一七六九1769视频免费观看 | 亚洲欧美日韩中字综合 | 亚洲综合91 | 香蕉尹人 | 夜夜夜夜夜夜爽噜噜噜噜噜噜 | 日本不卡视频在线观看 | 久久综合久久自在自线精品自 | 欧美久久久久 | 国产精品一区久久 | 欧美日韩国产精品 | 亚洲精品久久玖玖玖玖 | 国产日韩欧美成人 | 日本精品一二三区 | 国产亚洲男人的天堂在线观看 | 国产精品欧美亚洲韩国日本不卡 |