1.背景描述
需要在pom文件中添加的jar包
将entity中的属性,赋值给model,或者是vo.这样如果用之前的一个个的**entity.setId(**model.getId)这样的方法,将各个属性赋值会特别麻烦.所以封装了BeanMapperUtil这个类.一共两个方法,一个方法是map(返回entity),一个是mapList(返回List<entity>).
2.代码实现
package com.dmsdbj.itoo.examinationEvaluation.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.dozer.DozerBeanMapper;
/**
* Created by chenxiaochan on 2017/7/17.
*/
public class BeanMapperUtil {
private static DozerBeanMapper dozer = new DozerBeanMapper();
public static <T> T map(Object sourceObject, Class<T> destObjectclazz) {
return sourceObject == null ? null : dozer.map(sourceObject, destObjectclazz);
}
public static <T, S> List<T> mapList(Collection<S> sourceList, Class<T> destObjectclazz) {
if (sourceList == null) {
return null;
}
List<T> destinationList = new ArrayList<T>();
for (Iterator<S> it = sourceList.iterator(); it.hasNext();) {
destinationList.add(map(it.next(), destObjectclazz));
}
return destinationList;
}
}
需要在pom文件中添加的jar包
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.5.1</version>
</dependency>