通过BeanUtils功能,手动封装一下,实现多对象拷贝
- 继承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