有时候我们在持久层,需要将list集合类型的DTO对象转化为VO给web层使用,我们就可以使用如下方式进行转换后,然后再给Web层
public class BeanCopyUtil extends BeanUtils {
/**
* 集合数据的拷贝
* @param sources: 数据源类
* @param target: 目标类::new
* eg: BeanCopyUtil.copyListProperties(selectList,UserInfo::new);
* @return
*/
public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
List<T> list = new ArrayList<>();
sources.forEach(v->{
T t = target.get();
copyProperties(v, t);
list.add(t);
});
return list;
}
}