import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
public class MyBeanUtils extends BeanUtils {
public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
return copyListProperties(sources, target, null);
}
public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target, MyBeanUtilsCallBack<S, T> callBack) {
List<T> list = new ArrayList<>(sources.size());
for (S source : sources) {
T t = target.get();
copyProperties(source, t);
if (callBack != null) {
// 回调
callBack.callBack(source, t);
}
list.add(t);
}
return list;
}
@FunctionalInterface
public interface MyBeanUtilsCallBack<S, T> {
void callBack(S t, T s);
}
}
//调用方式如下:
//
// Copy
// List<AdminEntity> adminList = ...;
// List<ArticleVo> articleVoList = ColaBeanUtils.copyListProperties(adminList, AdminVo::new);
// return articleVoList;
// 如果需要在循环中做处理(回调),那么可使用lambda表达式:
//
// Copy
// List<Article> adminEntityList = articleMapper.getAllArticle();
// List<ArticleVo> articleVoList = ColaBeanUtils.copyListProperties(adminEntityList , ArticleVo::new, (articleEntity, articleVo) -> {
// // 回调处理
// });
// return articleVoList;