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>