JAVA進階:VO(DTO)與PO(DAO)之間的轉換

文章轉自:http://marsvaadin.iteye.com/blog/1294199

PO即 Persistence Object
  VO即 Value Object

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

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

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

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

 

 

Java代碼  收藏代碼
  1. public static void copyProperties(java.lang.Object dest,  
  2.                                   java.lang.Object orig)  
  3.                            throws java.lang.IllegalAccessException,  
  4.                                   java.lang.reflect.InvocationTargetExceptioCopy property values from the origin bean to the destination bean for all cases where the property names are the same.  

 

Java代碼  收藏代碼
  1. TUser user  =   new  TUser();  
  2. TUser anotherUser  =   new  TUser();  
  3. user.setName( " Emma " );  
  4. user.setUserType( 1 );  
  5.  try      {  
  6. BeanUtils.copyProperties(anotherUser,user);  
  7. System.out.println( " UserName =>  "   
  8. + anotherUser.getName()  
  9. );  
  10. System.out.println( " UserType =>  "   
  11. +  anotherUser.getUserType()  
  12. );  
  13.  }   catch  (IllegalAccessException e)    {  
  14. e.printStackTrace();  
  15.  }   catch  (InvocationTargetException e)    {  
  16. e.printStackTrace();  
  17. }    

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

 

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

 可以看到,在範例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中則可能是此對象的全部屬性。其中的一些轉換則需要依據前臺需要針對性地處理啦!


發佈了22 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章