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

  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. public class BeanMapper
  7. {
  8. private static DozerBeanMapper dozer = new DozerBeanMapper();
  9. /**
  10. * 构造新的destinationClass实例对象,通过source对象中的字段内容
  11. * 映射到destinationClass实例对象中,并返回新的destinationClass实例对象。
  12. *
  13. * @param source 源数据对象
  14. * @param destinationClass 要构造新的实例对象Class
  15. */
  16. public static <T> T map(Object source, Class<T> destinationClass)
  17. {
  18. return dozer.map(source, destinationClass);
  19. }
  20. public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass)
  21. {
  22. List destinationList = Lists.newArrayList();
  23. for (Iterator i$ = sourceList.iterator(); i$.hasNext(); ) { Object sourceObject = i$.next();
  24. Object destinationObject = dozer.map(sourceObject, destinationClass);
  25. destinationList.add(destinationObject);
  26. }
  27. return destinationList;
  28. }
  29. /**
  30. * 将对象source的所有属性值拷贝到对象destination中.
  31. *
  32. * @param source 对象source
  33. * @param destination 对象destination
  34. */
  35. public static void copy(Object source, Object destinationObject)
  36. {
  37. dozer.map(source, destinationObject);
  38. }
  39. }

使用:

  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对象。

    1. public static <T> Map<String, T> toMap(Object target) {
    2. return toMap(target,false);
    3. }
    4. /**
    5. * 将目标对象的所有属性转换成Map对象
    6. *
    7. * @param target 目标对象
    8. * @param ignoreParent 是否忽略父类的属性
    9. *
    10. * @return Map
    11. */
    12. public static <T> Map<String, T> toMap(Object target,boolean ignoreParent) {
    13. return toMap(target,ignoreParent,false);
    14. }
    15. /**
    16. * 将目标对象的所有属性转换成Map对象
    17. *
    18. * @param target 目标对象
    19. * @param ignoreParent 是否忽略父类的属性
    20. * @param ignoreEmptyValue 是否不把空值添加到Map中
    21. *
    22. * @return Map
    23. */
    24. public static <T> Map<String, T> toMap(Object target,boolean ignoreParent,boolean ignoreEmptyValue) {
    25. return toMap(target,ignoreParent,ignoreEmptyValue,new String[0]);
    26. }
    27. /**
    28. * 将目标对象的所有属性转换成Map对象
    29. *
    30. * @param target 目标对象
    31. * @param ignoreParent 是否忽略父类的属性
    32. * @param ignoreEmptyValue 是否不把空值添加到Map中
    33. * @param ignoreProperties 不需要添加到Map的属性名
    34. */
    35. public static <T> Map<String, T> toMap(Object target,boolean ignoreParent,boolean ignoreEmptyValue,String... ignoreProperties) {
    36. Map<String, T> map = new HashMap<String, T>();
    37. List<Field> fields = ReflectionUtils.getAccessibleFields(target.getClass(), ignoreParent);
    38. for (Iterator<Field> it = fields.iterator(); it.hasNext();) {
    39. Field field = it.next();
    40. T value = null;
    41. try {
    42. value = (T) field.get(target);
    43. } catch (Exception e) {
    44. e.printStackTrace();
    45. }
    46. if (ignoreEmptyValue
    47. && ((value == null || value.toString().equals(""))
    48. || (value instanceof Collection && ((Collection<?>) value).isEmpty())
    49. || (value instanceof Map && ((Map<?,?>)value).isEmpty()))) {
    50. continue;
    51. }
    52. boolean flag = true;
    53. String key = field.getName();
    54. for (String ignoreProperty:ignoreProperties) {
    55. if (key.equals(ignoreProperty)) {
    56. flag = false;
    57. break;
    58. }
    59. }
    60. if (flag) {
    61. map.put(key, value);
    62. }
    63. }
    64. return map;
    65. }
 
 

最新文章

  1. Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp
  2. 用CentOS 7打造合适的科研环境 :zhuan
  3. Swift枚举|结构体|类|属性|方法|下标脚本|继承
  4. PHPUnit测试
  5. Delphi 对象的创建(create)与释放(free/destory)
  6. UVA 11549 Calculator Conundrum (Floyd判圈算法)
  7. Django__WSGI
  8. Linux时间子系统之(十四):tick broadcast framework
  9. 使用Zabbix监控mysql的主从同步
  10. ggplot
  11. 6 Prefer and Would rather
  12. Excel转datatable
  13. SpringBoot使用其他的Servlet容器
  14. 解决java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor问题
  15. centos 7.4安装教程
  16. 手动搭建ABP2.1.3 Zero——基础框架
  17. SQL Server - 最佳实践 - 参数嗅探问题 转。
  18. 使用IIS实现反向代理
  19. 【转载】Remote System Explorer Operation总是运行后台服务,卡死eclipse解决办法
  20. [Erlang35]Erlang18的time

热门文章

  1. Cinder配置多Ceph后端步骤
  2. 使用supervisor方便调试程序
  3. CodeForces 570E DP Pig and Palindromes
  4. 认识Function.prototype.call
  5. python week08 并发编程之多线程--理论部分
  6. Android 程序 LinearLayout布局 参数layout_weight 探讨
  7. 九度oj 1003
  8. 【bzoj2729】[HNOI2012]排队 组合数学+高精度
  9. 【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp
  10. NOIP2017赛前模拟(4):总结