DozerBeanMapper + 对象转Map方法

 

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

转载于:https://www.cnblogs.com/shihaiming/p/7592101.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值