DozerBeanMapper + 對象轉Map方法

來源:互聯網
上載者:User

標籤:false   bar   break   turn   ati   值拷貝   shm   雜類   amp   

 

1、簡介 
    dozer是一種JavaBean的映射工具,類似於apache的BeanUtils。但是dozer更強大,它可以靈活的處理複雜類型之間的映射。不但可以進行簡單的屬性對應、複雜的類型映射、雙向映射、遞迴映射等,並且可以通過XML設定檔進行靈活的配置。 

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

 

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

 

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. }  

 

 

使用:

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)

轉換為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.     }  

DozerBeanMapper + 對象轉Map方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.