springboot bean拷贝工具类
import org.springframework.beans.BeanUtils;
import java.util.List;
import java.util.stream.Collectors;
public class BeanCopyUtil {
public BeanCopyUtil() {
}
public static <V> V copyBean(Object source, Class<V> targetClass) {
V result = null;
try {
result = targetClass.newInstance();
BeanUtils.copyProperties(source, result);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static <O,V> List<V> copyBeanList(List<O> list, Class<V> targetClass) {
List<V> result = list.stream()
.map(o -> copyBean(o, targetClass))
.collect(Collectors.toList());
return result;
}
}
List<ArticleVo> articleVos = BeanCopyUtil.copyBeanList(articles, ArticleVo.class);