对象拷贝 工具类 BeansCopyUtils

该博客介绍如何利用Hutool库中的BeanUtil工具类进行对象属性的复制,包括从一个对象列表复制到另一个对象列表,以及提供回调方法进行自定义处理。示例代码展示了从UserEntity列表转换为UserVO列表的过程,并提供了回调接口用于处理转换过程中的特殊需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

依赖

  <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.4.2</version>
        </dependency>

package com.jsy.basic.util.utils;

import cn.hutool.core.bean.BeanUtil;

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

import static cn.hutool.core.bean.BeanUtil.copyProperties;

public final   class BeansCopyUtils {
    private BeansCopyUtils() {
    }

    public static <T1, T2> List<T2> listCopy(List<T1> sourceList, Class<T2> clazz) {
        return (List<T2>) sourceList.stream().map((source)->{
            Object target;
            try {
                target = clazz.getDeclaredConstructor().newInstance();
            } catch (Exception e) {
                throw new RuntimeException();
            }
            copyProperties(source, target);
            return target;
        }).collect(Collectors.toList());
    }
    public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
        return copyListProperties(sources, target, null);
    }

    /**
     * <p>带回调参数的集合数据的拷贝</p>
     *
     * @param sources  数据源类
     * @param target   目标类::new (eg: UserVO::new)
     * @param callBack 回调函数 eg: BeanCopyUtil.copyListProperties(userLiset,User:: new, (userDo,userVo) ->{
     *                 userVo.setSex()})
     * @return List<T> List<Bean>
     * @author renkaijiang
     * @Date 2020/6/19 15:09
     */
    public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target, BeanCopyUtilCallBack<S, T> callBack) {
        List<T> list = new ArrayList<>(sources.size());
        for (S source : sources) {
            T t = target.get();
            copyProperties(source, t);
            list.add(t);
            if (callBack != null) {
                callBack.callBack(source, t);
            }
        }
        return list;
    }
    /**
     * @Description 默认回调方法
     * @ClassName BeanCopyUtilCallBack
     * @Author renkaijiang
     * @Date 2020/6/19 14:22
     * @Version 1.0
     */
    public interface BeanCopyUtilCallBack<S, T> {
        void callBack(S s, T t);
    }

 /**
  //原数据
  List<UserEntity> ue = userService.getUser();

  //不需要特殊处理,所以不写回调方法
  List<UserVO> user = BeanCopyUtil.copyListProperties(ue,UserVO::new);


  //有参数不一致或者需要处理的,例如UserVO的name = UserEntity的userName,
  //状态需要转化
  List<UserVO> user = BeanCopyUtil.copyListProperties(ue,UserVO::new,
  (userEntity,userVO) ->{
  userVO.setName(userEntity.getUserName());

  userVo.setStatus(userEntity.getStatus==1?"正常":"锁定");



  });


  */



    public static void main(String[] args) {
        List list= new ArrayList<UserDO>();
        UserDO userDO = new UserDO("1","李四",19,"男");
        UserDO userDO1 = new UserDO("2","张三",18,"男");
        list.add(userDO);
        list.add(userDO1);
        List list1 = BeansCopyUtils.listCopy(list, UserDTO.class);
        System.out.println(list1.toString());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员小小刘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值