開發(fā)中,規(guī)范不能用mapper查出來的結(jié)果直接傳念赶,而是需要定義專門用來傳數(shù)據(jù)的DTO對象
這時候查出來的數(shù)據(jù)就需要用BeanUtil的copyProperties來轉(zhuǎn)一下過去,List略麻煩恰力,遂寫個通用工具
先引個萬能hutool省事
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.0.6</version>
</dependency>
/**
* @Author: Fcx
* @Date: 2019/11/20 20:45
* @Version 1.0
*/
public class CopyListUtil {
private CopyListUtil() {
}
/**
* 列表對象拷貝
* @param sources 源列表
* @param clazz 目標列表對象Class
* @param <T> 目標列表對象類型
* @param <M> 源列表對象類型
* @return 目標列表
*/
public static <T, M> List<T> copyListProperties(List<M> sources, Class<T> clazz) {
if (Objects.isNull(sources) || Objects.isNull(clazz) || sources.isEmpty()) {
throw new IllegalArgumentException();
}
List<T> targets = new ArrayList<>(sources.size());
for (M source : sources) {
T t = ReflectUtil.newInstance(clazz);
BeanUtil.copyProperties(source,t);
targets.add(t);
}
return targets;
}
}
開發(fā)注意規(guī)范哦