DozerBeanMapper + 對象轉Map方法

 

一、簡介 
    dozer是一種JavaBean的映射工具,相似於apache的BeanUtils。可是dozer更強大,它能夠靈活的處理複雜類型之間的映射。不但能夠進行簡單的屬性映射、複雜的類型映射、雙向映射、遞歸映射等,而且能夠經過XML配置文件進行靈活的配置。 

二、準備 
   如今開始就小試一下。 
   首先,須要下載jar包, 
   dozer.jar :http://dozer.sourceforge.net/downloading.html 
   還須要slf4j.jar,commons-lang.jar,commons-beanutil.jar, commons-loggin.jar html

 

http://lishaorui.iteye.com/blog/1151513java

 

Java代碼  
  1. import com.google.common.collect.Lists;  
  2. import java.util.Collection;  
  3. import java.util.Iterator;  
  4. import java.util.List;  
  5. import org.dozer.DozerBeanMapper;  
  6.   
  7. public class BeanMapper  
  8. {  
  9.   private static DozerBeanMapper dozer = new DozerBeanMapper();  
  10.   
  11.   /** 
  12.   * 構造新的destinationClass實例對象,經過source對象中的字段內容 
  13.   * 映射到destinationClass實例對象中,並返回新的destinationClass實例對象。 
  14.   *  
  15.   * @param source 源數據對象 
  16.   * @param destinationClass 要構造新的實例對象Class 
  17.   */  
  18.   public static <T> T map(Object source, Class<T> destinationClass)  
  19.   {  
  20.     return dozer.map(source, destinationClass);  
  21.   }  
  22.     
  23.     
  24.   
  25.   public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass)  
  26.   {  
  27.     List destinationList = Lists.newArrayList();  
  28.     for (Iterator i$ = sourceList.iterator(); i$.hasNext(); ) { Object sourceObject = i$.next();  
  29.       Object destinationObject = dozer.map(sourceObject, destinationClass);  
  30.       destinationList.add(destinationObject);  
  31.     }  
  32.     return destinationList;  
  33.   }  
  34.   
  35.     
  36.   /** 
  37.   * 將對象source的全部屬性值拷貝到對象destination中. 
  38.   *  
  39.   * @param source 對象source 
  40.   * @param destination 對象destination 
  41.   */  
  42.   public static void copy(Object source, Object destinationObject)  
  43.   {  
  44.     dozer.map(source, destinationObject);  
  45.   }  
  46. }  

 

 

使用:apache

Java代碼  
  1. SmIaasQuotaV result = null;  
  2.         try {  
  3.             result = limitService.getLimitDetails(id,parentId);  
  4.             if(result != null){  
  5.                 response.setData(BeanMapper.map(result, Map.class));  
  6.                 response.setSuccess(true);  
  7.             }  
  8.         }  

BeanMapper.map(result, Map.class)app

轉換爲Map對象。工具

 

 

 

Java代碼  
    1. public static <T> Map<String, T> toMap(Object target) {  
    2.     return toMap(target,false);  
    3. }  
    4.   
    5. /** 
    6.  * 將目標對象的全部屬性轉換成Map對象 
    7.  *  
    8.  * @param target 目標對象 
    9.  * @param ignoreParent 是否忽略父類的屬性 
    10.  *  
    11.  * @return Map 
    12.  */  
    13. public static <T> Map<String, T> toMap(Object target,boolean ignoreParent) {  
    14.     return toMap(target,ignoreParent,false);  
    15. }  
    16.   
    17. /** 
    18.  * 將目標對象的全部屬性轉換成Map對象 
    19.  *  
    20.  * @param target 目標對象 
    21.  * @param ignoreParent 是否忽略父類的屬性 
    22.  * @param ignoreEmptyValue 是否不把空值添加到Map中 
    23.  *  
    24.  * @return Map 
    25.  */  
    26. public static <T> Map<String, T> toMap(Object target,boolean ignoreParent,boolean ignoreEmptyValue) {  
    27.     return toMap(target,ignoreParent,ignoreEmptyValue,new String[0]);  
    28. }  
    29.   
    30. /** 
    31.  * 將目標對象的全部屬性轉換成Map對象 
    32.  *  
    33.  * @param target 目標對象 
    34.      * @param ignoreParent 是否忽略父類的屬性 
    35.      * @param ignoreEmptyValue 是否不把空值添加到Map中 
    36.      * @param ignoreProperties 不須要添加到Map的屬性名 
    37.  */  
    38.     public static <T> Map<String, T> toMap(Object target,boolean ignoreParent,boolean ignoreEmptyValue,String... ignoreProperties) {  
    39.         Map<String, T> map = new HashMap<String, T>();  
    40.           
    41.     List<Field> fields = ReflectionUtils.getAccessibleFields(target.getClass(), ignoreParent);  
    42.       
    43.     for (Iterator<Field> it = fields.iterator(); it.hasNext();) {  
    44.         Field field = it.next();  
    45.         T value = null;  
    46.           
    47.         try {  
    48.             value = (T) field.get(target);  
    49.         } catch (Exception e) {  
    50.             e.printStackTrace();  
    51.         }  
    52.           
    53.         if (ignoreEmptyValue  
    54.                 && ((value == null || value.toString().equals(""))  
    55.                 || (value instanceof Collection && ((Collection<?>) value).isEmpty())  
    56.                 || (value instanceof Map && ((Map<?,?>)value).isEmpty()))) {  
    57.             continue;  
    58.         }  
    59.           
    60.         boolean flag = true;  
    61.         String key = field.getName();  
    62.           
    63.         for (String ignoreProperty:ignoreProperties) {  
    64.             if (key.equals(ignoreProperty)) {  
    65.                 flag = false;  
    66.                 break;  
    67.             }  
    68.         }  
    69.           
    70.         if (flag) {  
    71.             map.put(key, value);  
    72.         }  
    73.     }  
    74.       
    75.         return map;  
    76.     }  
相關文章
相關標籤/搜索