PO 转 VO 复制

  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,应该写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,应该写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代码 复制代码
  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. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值