BeanUtils处理list集合数据转换

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  T convertTo(S source, Supplier targetSupplier) {
        return convertTo(source, targetSupplier, null);
    }

    /**
     * 转换对象
     *
     * @param source         源对象
     * @param targetSupplier 目标对象供应方
     * @param callBack       回调方法
     * @param             源对象类型
     * @param             目标对象类型
     * @return 目标对象
     */
    public static  T convertTo(S source, Supplier targetSupplier, ConvertCallBack 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  List convertListTo(List sources, Supplier targetSupplier) {
        return convertListTo(sources, targetSupplier, null);
    }

    /**
     * 转换对象
     *
     * @param sources        源对象list
     * @param targetSupplier 目标对象供应方
     * @param callBack       回调方法
     * @param             源对象类型
     * @param             目标对象类型
     * @return 目标对象list
     */
    public static  List convertListTo(List sources, Supplier targetSupplier, ConvertCallBack callBack) {
        if (null == sources || null == targetSupplier) {
            return null;
        }

        List 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  源对象类型
     * @param  目标对象类型
     */
    @FunctionalInterface
    public interface ConvertCallBack {
        void callBack(S t, T s);
    }
}

测试类
//EntAppealDTO和AppealInitateBO字段属性一样,自己随意DIY吧。
 List entAppealDTO=new ArrayList<>();
        EntAppealDTO dto=new EntAppealDTO();
        dto.setAppealDate(new Date());
        List entAppealFilesDTOList=new ArrayList<>();
        EntAppealFilesDTO filesDTO=new EntAppealFilesDTO();
        filesDTO.setFileId("123456");
        entAppealFilesDTOList.add(filesDTO);
        dto.setEntAppealFilesDTOList(entAppealFilesDTOList);
        entAppealDTO.add(dto);
        //通过lambda表达式特殊处理个别字段
        List initateBOList=aaaaa.convertListTo(entAppealDTO,AppealInitateBO::new,(s, t) -> t.setCityCode(s.getEntAppealFilesDTOList().get(0).getFileId()));
        //不带单独处理
        //List initateBOList=aaaaa.convertListTo(entAppealDTO,AppealInitateBO::new);
        System.out.println(initateBOList);
工具类2
//单独实体类拷贝转换
 public static  T toDtoOrInfo(F f, T d){
        BeanUtils.copyProperties(f,d);
        return d;
    }
//嵌套list实体类拷贝转换
    public static  List toInfoList(List sList, Class tClass){
        List 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)

你可能感兴趣的:(BeanUtils处理list集合数据转换)