引入依赖
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>${dozer.version}</version>
</dependency>
包装实现类
package com.dossen.store.workbench.web.testdata.utils;
import org.dozer.DozerBeanMapper;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* 深度复制映射转换辅助类
*
* @author flydoos
* @date 2019/4/3
*/
public final class MapperUtils {
/**
* 饿汉模式实现单例
*/
private static DozerBeanMapper beanMapper = new DozerBeanMapper();
/**
* 普通对象转换 比如: ADO -> AVO
*
* @param: [source 源对象, targetClass 目标对象class]
* @return: T
*/
public static <T> T map(Object source, Class<T> targetClass) {
if (source == null) {
return null;
}
return beanMapper.map(source, targetClass);
}
/**
* List转换 比如: List<ADO> -> List<AVO>
*
* @param: [sourceList 源对象List, targetClass 目标对象class]
* @return: java.util.List<T>
*/
public static <T> List<T> mapList(Collection sourceList, Class<T> targetClass) {
List<T> targetList = new ArrayList();
if (sourceList == null) {
return targetList;
}
for (Object sourceObject : sourceList) {
if (sourceObject != null) {
T targetObject = beanMapper.map(sourceObject, targetClass);
targetList.add(targetObject);
}
}
return targetList;
}
}