BeanUtils处理list集合数据转换
2022-11-04 本文已影响0人
楼兰King
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
public class aaaaa extends BeanUtils{
public static <S, T> T convertTo(S source, Supplier<T> targetSupplier) {
return convertTo(source, targetSupplier, null);
}
/**
* 转换对象
*
* @param source 源对象
* @param targetSupplier 目标对象供应方
* @param callBack 回调方法
* @param <S> 源对象类型
* @param <T> 目标对象类型
* @return 目标对象
*/
public static <S, T> T convertTo(S source, Supplier<T> targetSupplier, ConvertCallBack<S, T> callBack) {
if (null == source || null == targetSupplier) {
return null;
}
T target = targetSupplier.get();
BeanUtils.copyProperties(source, target);
if (callBack != null) {
callBack.callBack(source, target);
}
return target;
}
public static <S, T> List<T> convertListTo(List<S> sources, Supplier<T> targetSupplier) {
return convertListTo(sources, targetSupplier, null);
}
/**
* 转换对象
*
* @param sources 源对象list
* @param targetSupplier 目标对象供应方
* @param callBack 回调方法
* @param <S> 源对象类型
* @param <T> 目标对象类型
* @return 目标对象list
*/
public static <S, T> List<T> convertListTo(List<S> sources, Supplier<T> targetSupplier, ConvertCallBack<S, T> callBack) {
if (null == sources || null == targetSupplier) {
return null;
}
List<T> list = new ArrayList<>(sources.size());
for (S source : sources) {
T target = targetSupplier.get();
BeanUtils.copyProperties(source, target);
if (callBack != null) {
callBack.callBack(source, target);
}
list.add(target);
}
return list;
}
/**
* 回调接口
*
* @param <S> 源对象类型
* @param <T> 目标对象类型
*/
@FunctionalInterface
public interface ConvertCallBack<S, T> {
void callBack(S t, T s);
}
}
测试类
//EntAppealDTO和AppealInitateBO字段属性一样,自己随意DIY吧。
List<EntAppealDTO> entAppealDTO=new ArrayList<>();
EntAppealDTO dto=new EntAppealDTO();
dto.setAppealDate(new Date());
List<EntAppealFilesDTO> entAppealFilesDTOList=new ArrayList<>();
EntAppealFilesDTO filesDTO=new EntAppealFilesDTO();
filesDTO.setFileId("123456");
entAppealFilesDTOList.add(filesDTO);
dto.setEntAppealFilesDTOList(entAppealFilesDTOList);
entAppealDTO.add(dto);
//通过lambda表达式特殊处理个别字段
List<AppealInitateBO> initateBOList=aaaaa.convertListTo(entAppealDTO,AppealInitateBO::new,(s, t) -> t.setCityCode(s.getEntAppealFilesDTOList().get(0).getFileId()));
//不带单独处理
//List<AppealInitateBO> initateBOList=aaaaa.convertListTo(entAppealDTO,AppealInitateBO::new);
System.out.println(initateBOList);
工具类2
//单独实体类拷贝转换
public static <T,F> T toDtoOrInfo(F f, T d){
BeanUtils.copyProperties(f,d);
return d;
}
//嵌套list实体类拷贝转换
public static <S,T> List<T> toInfoList(List<S> sList, Class<T> tClass){
List<T> tList = new ArrayList<>();
try {
Constructor<?> constructor = tClass.getConstructor();
sList.forEach( s -> {
Object o;
try {
o = constructor.newInstance();
tList.add((T)toDtoOrInfo(s, o));
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
});
} catch ( NoSuchMethodException e) {
e.printStackTrace();
}
return tList;
}
//使用
toDtoOrInfo(源实体类,new 目标实体类)
toInfoList(源实体类,目标实体类.class)