java对象拷贝,对象数组拷贝封装

通过BeanUtils功能,手动封装一下,实现多对象拷贝

  1. 继承BeanUtils
    package com.example.start.common.utils.beancopyutil;
    
    import org.springframework.beans.BeanUtils;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.function.Supplier;
    
    /**
     * @Author luckylittle
     * @Date 20:15 2020/12/5
     * @Description
     */
    public class BeanCopyUtils extends BeanUtils {
        /**
         * 集合数据的拷贝
         *
         * @param sources: 数据源类
         * @param target: 目标类::new(eg: UserVO::new)
         * @return
         */
        public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
            return copyListProperties(sources, target, null);
        }
    
        /**
         * 带回调函数的集合数据的拷贝(可自定义字段拷贝规则)
         *
         * @param sources: 数据源类
         * @param target: 目标类::new(eg: UserVO::new)
         * @param callBack: 回调函数
         * @return
         */
        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;
        }
    }
    

 2.定义默认回调方法

package com.example.start.common.utils.beancopyutil;

/**
 * @Author luckylittle
 * @Date 20:20 2020/12/5
 * @Description
 */
@FunctionalInterface
public interface BeanCopyUtilCallBack<S, T> {
    /**
     * 定义默认回调方法
     *
     * @param s: 数据源类 ,
     * @param t: 目标类
     */
    void callBack(S s, T t);
}

3.测试用的实体类

public class User {
    private String name;
    private String age;
    private String sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}




public class UserDto {
    private String name;
    private String age;
    private String password;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

4.测试

package com.example.start.common.utils.beancopyutil;


import java.util.Arrays;
import java.util.List;

/**
 * @Author luckylittle
 * @Date 20:25 2020/12/5
 * @Description
 */
public class BeanCopyTest {
    public static void main(String[] args) {
        // 拷贝单个对象
        UserDto userDto = new UserDto();
        userDto.setName("张三");
        userDto.setAge("11");
        userDto.setPassword("123456");
        User user = new User();
        BeanCopyUtils.copyProperties(userDto, user);
        System.out.println("单个对象拷贝" + "姓名:" + user.getName() + "年龄:" + user.getAge() + "性别:" + user.getSex());

        // 拷贝List
        UserDto dto1 = new UserDto();
        dto1.setName("张三");
        dto1.setAge("11");
        dto1.setPassword("123456");
        UserDto dto2 = new UserDto();
        dto2.setName("李四");
        dto2.setAge("22");
        dto2.setPassword("123456");
        List<UserDto> userDtosList = Arrays.asList(dto1, dto2);
        List<User> users = BeanCopyUtils.copyListProperties(userDtosList, User::new);
        users.stream().forEach(
                item -> System.out.println("对象数组拷贝:" + "姓名:" + item.getName() + "年龄:" + item.getAge() + "性别:" + item.getSex())
        );

        // 带回调函数数组拷贝
        List<User> userlist = BeanCopyUtils.copyListProperties(userDtosList, User::new,
                (userDto1, user1) -> user1.setSex(userDto1.getPassword())
        );
        userlist.stream().forEach(
                item -> System.out.println("回调数组拷贝:" + "姓名:" + item.getName() + "年龄:" + item.getAge() + "性别:" + item.getSex())
        );
    }
}

5.测试结果   拷贝对象如果属性不一样不会自动拷贝,需要手动处理

单个对象拷贝姓名:张三年龄:11性别:null
对象数组拷贝:姓名:张三年龄:11性别:null
对象数组拷贝:姓名:李四年龄:22性别:null
回调数组拷贝:姓名:张三年龄:11性别:123456
回调数组拷贝:姓名:李四年龄:22性别:123456

 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 59
    评论
评论 59
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值