java dozer map转对象_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

import com.google.common.collect.Lists;

import java.util.Collection;

import java.util.Iterator;

import java.util.List;

import org.dozer.DozerBeanMapper;

public class BeanMapper

{

private static DozerBeanMapper dozer = new DozerBeanMapper();

/**

* 构造新的destinationClass实例对象,通过source对象中的字段内容

* 映射到destinationClass实例对象中,并返回新的destinationClass实例对象。

*

* @param source 源数据对象

* @param destinationClass 要构造新的实例对象Class

*/

public static T map(Object source, Class destinationClass)

{

return dozer.map(source, destinationClass);

}

public static List mapList(Collection sourceList, Class destinationClass)

{

List destinationList = Lists.newArrayList();

for (Iterator i$ = sourceList.iterator(); i$.hasNext(); ) { Object sourceObject = i$.next();

Object destinationObject = dozer.map(sourceObject, destinationClass);

destinationList.add(destinationObject);

}

return destinationList;

}

/**

* 将对象source的所有属性值拷贝到对象destination中.

*

* @param source 对象source

* @param destination 对象destination

*/

public static void copy(Object source, Object destinationObject)

{

dozer.map(source, destinationObject);

}

}

使用:

SmIaasQuotaV result = null;

try {

result = limitService.getLimitDetails(id,parentId);

if(result != null){

response.setData(BeanMapper.map(result, Map.class));

response.setSuccess(true);

}

}

BeanMapper.map(result, Map.class)

转换为Map对象。

public static Map toMap(Object target) {

return toMap(target,false);

}

/**

* 将目标对象的所有属性转换成Map对象

*

* @param target 目标对象

* @param ignoreParent 是否忽略父类的属性

*

* @return Map

*/

public static Map toMap(Object target,boolean ignoreParent) {

return toMap(target,ignoreParent,false);

}

/**

* 将目标对象的所有属性转换成Map对象

*

* @param target 目标对象

* @param ignoreParent 是否忽略父类的属性

* @param ignoreEmptyValue 是否不把空值添加到Map中

*

* @return Map

*/

public static Map toMap(Object target,boolean ignoreParent,boolean ignoreEmptyValue) {

return toMap(target,ignoreParent,ignoreEmptyValue,new String[0]);

}

/**

* 将目标对象的所有属性转换成Map对象

*

* @param target 目标对象

* @param ignoreParent 是否忽略父类的属性

* @param ignoreEmptyValue 是否不把空值添加到Map中

* @param ignoreProperties 不需要添加到Map的属性名

*/

public static Map toMap(Object target,boolean ignoreParent,boolean ignoreEmptyValue,String... ignoreProperties) {

Map map = new HashMap();

List fields = ReflectionUtils.getAccessibleFields(target.getClass(), ignoreParent);

for (Iterator it = fields.iterator(); it.hasNext();) {

Field field = it.next();

T value = null;

try {

value = (T) field.get(target);

} catch (Exception e) {

e.printStackTrace();

}

if (ignoreEmptyValue

&& ((value == null || value.toString().equals(""))

|| (value instanceof Collection && ((Collection>) value).isEmpty())

|| (value instanceof Map && ((Map,?>)value).isEmpty()))) {

continue;

}

boolean flag = true;

String key = field.getName();

for (String ignoreProperty:ignoreProperties) {

if (key.equals(ignoreProperty)) {

flag = false;

break;

}

}

if (flag) {

map.put(key, value);

}

}

return map;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值