`
uule
  • 浏览: 6305020 次
  • 性别: Icon_minigender_1
  • 来自: 一片神奇的土地
社区版块
存档分类
最新评论

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> T map(Object source, Class<T> destinationClass)
  {
    return dozer.map(source, destinationClass);
  }
  
  

  public static <T> List<T> mapList(Collection sourceList, Class<T> 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 <T> Map<String, T> toMap(Object target) {
		return toMap(target,false);
	}
	
	/**
	 * 将目标对象的所有属性转换成Map对象
	 * 
	 * @param target 目标对象
	 * @param ignoreParent 是否忽略父类的属性
	 * 
	 * @return Map
	 */
	public static <T> Map<String, T> toMap(Object target,boolean ignoreParent) {
		return toMap(target,ignoreParent,false);
	}
	
	/**
	 * 将目标对象的所有属性转换成Map对象
	 * 
	 * @param target 目标对象
	 * @param ignoreParent 是否忽略父类的属性
	 * @param ignoreEmptyValue 是否不把空值添加到Map中
	 * 
	 * @return Map
	 */
	public static <T> Map<String, T> 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 <T> Map<String, T> toMap(Object target,boolean ignoreParent,boolean ignoreEmptyValue,String... ignoreProperties) {
  		Map<String, T> map = new HashMap<String, T>();
  		
		List<Field> fields = ReflectionUtils.getAccessibleFields(target.getClass(), ignoreParent);
		
		for (Iterator<Field> 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;
  	}

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics