对BeanUtils进行封装Object|List<Object>|Page<Object>,看这篇就够了

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public class BeanConvertUtils extends BeanUtils {

    public static <S, T> T convertTo(S source, Supplier<T> targetSupplier) {
        return convertTo(source, targetSupplier, null);
    }

    /**
     * 转换对象
     * <p>
     *
     * @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();
        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);
    }

    /**
     * 转换对象
     * <p>
     *
     * @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();
            copyProperties(source, target);
            if (callBack != null) {
                callBack.callBack(source, target);
            }
            list.add(target);
        }
        return list;
    }

    public static <S, T> PageDTO<T> convertPageTo(IPage<S> iPage, Supplier<T> targetSupplier) {
        return new PageDTO<>(iPage.getSize(), iPage.getCurrent(), iPage.getTotal(), iPage.getPages(), convertListTo(iPage.getRecords(), targetSupplier, null));
    }

    public static <S, T> PageDTO<T> convertPageTo(IPage<S> iPage, Supplier<T> targetSupplier, ConvertCallBack<S, T> callBack) {
        return new PageDTO<>(iPage.getSize(), iPage.getCurrent(), iPage.getTotal(), iPage.getPages(), convertListTo(iPage.getRecords(), targetSupplier, callBack));
    }

    /**
     * 回调接口
     * <p>
     *
     * @param <S> 源对象类型
     * @param <T> 目标对象类型
     */
    @FunctionalInterface
    public interface ConvertCallBack<S, T> {
        void callBack(S t, T s);
    }

    public static void getBean(StorehouseOutboundOrderDetailDTO detail, StorehouseOutboundOrderDeductionDTO deductionDTO) {
        deductionDTO.setOrderId(detail.getProductId());
        deductionDTO.setBatchInfo(detail.getImage());
    }

    public static void main(String[] args) {
        StorehouseOutboundOrderDetailDTO detail = new StorehouseOutboundOrderDetailDTO();
        detail.setVariantId("111111111");
        detail.setSku("222222222");
        detail.setSyncSiteId(869574);
        detail.setProductId("3333333333333");
        detail.setImage("{}");
        StorehouseOutboundOrderDeductionDTO deductionDTO = new StorehouseOutboundOrderDeductionDTO();
        System.out.println(JSON.toJSONString(convertTo(detail, StorehouseOutboundOrderDeductionDTO::new, (source, target) -> {
            target.setOrderId(source.getProductId());
        })));
        System.out.println(JSON.toJSONString(convertTo(detail, StorehouseOutboundOrderDeductionDTO::new, (p, t) -> getBean(p, t))));
        Page<StorehouseOutboundOrderDetailDTO> page = new Page<>();
        List<StorehouseOutboundOrderDetailDTO> list = Lists.newArrayList();
        StorehouseOutboundOrderDetailDTO detail1 = new StorehouseOutboundOrderDetailDTO();
        detail1.setVariantId("111111111");
        detail1.setSku("222222222");
        detail1.setSyncSiteId(869574);
        detail1.setProductId("3333333333333");
        detail1.setImage("{}");
        list.add(detail1);
        StorehouseOutboundOrderDetailDTO detail2 = new StorehouseOutboundOrderDetailDTO();
        detail2.setVariantId("T111111111");
        detail2.setSku("T222222222");
        detail2.setSyncSiteId(869574);
        detail2.setProductId("T3333333333333");
        detail2.setImage("{T}");
        list.add(detail2);
        page.setRecords(list);
        System.out.println(JSON.toJSONString(convertPageTo(page, StorehouseOutboundOrderDeductionDTO::new,(p, t) -> getBean(p, t))));
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值