引入依赖
<dependency>
<groupId>com.github.dozermapper</groupId>
<artifactId>dozer-core</artifactId>
<version>6.1.0</version>
</dependency>
import org.dozer.DozerBeanMapperBuilder;
import org.dozer.Mapper;
import org.dozer.MappingException;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class DozerBeanMapperUtils {
private static final Mapper mapper = DozerBeanMapperBuilder.buildDefault();
public static <T, S> T convert(S source, T target) throws MappingException {
if (source == null) {
return null;
}
Assert.notNull(target, "target instance required");
mapper.map(source, target);
return target;
}
public static <T, S> T convert(S source, Class<T> targetClass) throws MappingException {
if (source == null) {
return null;
}
Assert.notNull(targetClass, "targetClass required");
T target = mapper.map(source, targetClass);
return target;
}
public static <T, S> List<T> convert(List<S> source, Class<T> targetClass) throws MappingException {
if (source == null) {
return null;
}
List<T> targetList = new ArrayList<T>();
Iterator<S> iterator = source.iterator();
while (iterator.hasNext()) {
T target = mapper.map(iterator.next(), targetClass);
targetList.add(target);
}
return targetList;
}
}