zoukankan      html  css  js  c++  java
  • 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.     }  
     
     
  • 相关阅读:
    PowerDesigner小技巧(整理中)
    将日志(Microsoft.Extensions.Logging)添加到.NET Core控制台应用程序
    VMware Workstation Pro 15.5.0 官方版本及激活密钥
    Git 设置和取消代理(SOCKS5代理)
    笔记
    哈希表(Hash Table)与哈希算法
    Elasticsearch分词
    微服务理论
    Elasticsearch与Mysql数据同步
    go语言常用命令
  • 原文地址:https://www.cnblogs.com/shihaiming/p/7592101.html
Copyright © 2011-2022 走看看