往期目录
目录
1.1 将一个对象的相同属性值拷贝给另一个对象(一般情况:全拷贝);
1.4.1 解决方案1:使用 ignoreProperties。
1.4.2 解决方案2:使用 CopyOptions 对象。
主要用到 hutool工具的BeanUtil
依赖:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.6</version>
</dependency>
注:hutool工具版本过低,可能会少一些方法。
1.实体类对象拷贝
实体类准备
1.1 将一个对象的相同属性值拷贝给另一个对象(一般情况:全拷贝);
public class modelClass {
public static void main(String[] args) {
User user = User.builder()
.id("123")
.account("001")
.name("张三")
.mobile("12345678912")
.build();
System.out.println(user);//输出:User(id=123, account=001, name=张三, password=null,
mobile=12345678912)
UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
System.out.println(userDTO);//输出:UserDTO(ID=null, account=1, userName=null, password=null, mobile=12345678912, email=null)
UserDTO user1 = UserDTO.builder().password("123456").email("test.@123com").build();
BeanUtil.copyProperties(user, userDTO1);
System.out.println(userDTO1);//UserDTO(ID=null, account=1, userName=null, password=null, mobile=12345678912, email=test.@123com)
}
}
总结:只有属性名相同且数据类型相同的属性才会实现完全复制。
1.2 属性值为null不复制,保留原有值;
源对象user里password字段为null,目标对象user1里password字段为123456
System.out.println(user);//输出:User(id=123, account=001, name=张三, password=null,mobile=12345678912)
UserDTO user1 = UserDTO.builder().password("123456").email("test.@123com").build();
//配置选项 IgnoreNullValue 为true:忽略拷贝为null的属性。
BeanUtil.copyProperties(user, user1,CopyOptions.create().setIgnoreNullValue(true));
System.out.println(user1);//输出:UserDTO(ID=null, account=001, userName=null, password=123456, mobile=12345678912, email=test.@123com)
1.3 属性命名不同,存储的是相同值;
1.3.1 属性名大小写不同:忽略大小写拷贝。
System.out.println(user);//输出:User(id=1222, account=A001, name=张三, password=123456, mobile=12345678912)
UserDTO userDTO = new UserDTO();
BeanUtil.copyProperties(user, UserDTO.class,new CopyOptions().setIgnoreCase(true));
System.out.println(userDTO);//输出:UserDTO(ID=1222, account=A001, userName=null, password=123456, mobile=12345678912)
1.3.2 属性名拼写不同:配置拷贝字段映射选项。
System.out.println(user);//输出:User(account=A001, name=张三, password=123456, mobile=12345678912)
UserDTO userDTO = new UserDTO();
//配置拷贝选项
Map<String, String> mapping = new HashMap<>(1);
// key 为源字段,value为目标字段
mapping.put("name", "userName");
BeanUtil.copyProperties(user,userDTO, CopyOptions.create().setFieldMapping(mapping));
System.out.println(userDTO);//输出:UserDTO(account=A001, userName=张三, password=123456, mobile=12345678912)
1.4 实体类的属性名相同,但是数据类型不同;
给 account 赋值为 “001 ”,自动转换了,不报错(不建议!)。
public static void main(String[] args) {
User user = User.builder()
.account("001")
.name("张三")
.password("123456")
.mobile("12345678912")
.build();
System.out.println(user);//输出:User(account=001, name=张三, password=123456, mobile=12345678912)
UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
System.out.println(userDTO);//输出:UserDTO(account=1, name=张三, password=123456, mobile=12345678912)
UserDTO userDTO1 = new UserDTO();
BeanUtil.copyProperties(user, userDTO1);
System.out.println(userDTO1);//输出:UserDTO(account=1, name=张三, password=123456, mobile=12345678912)
}
给 account 赋值为 “A001 ”,不能实现转换,报错(如下)。
1.4.1 解决方案1:使用 ignoreProperties。
User user = User.builder()
.account("A001")
.name("张三")
.mobile("12345678912")
.build();
System.out.println(user);//输出:User(account=A001, name=张三, password=null, mobile=12345678912)
UserDTO user1 = UserDTO.builder().account(121L).password("123456").build();
//这里把为null也进行了忽略拷贝,也能实现为null不复制,保留原有值的效果
BeanUtil.copyProperties(user, user1,"account","password");
System.out.println(userDTO);//输出:UserDTO(account=121, userName=null, password=123456, mobile=12345678912, email=null)
1.4.2 解决方案2:使用 CopyOptions 对象。
System.out.println(user);//输出:User(account=A001, name=张三, password=123456, mobile=12345678912)
UserDTO user1 = new UserDTO();
//配置选项 IgnoreError 为true:忽略拷贝错误。
BeanUtil.copyProperties(user, user1,CopyOptions.create().setIgnoreError(true));
System.out.println(user1);//输出:UserDTO(account=null, name=张三, password=123456, mobile=12345678912)
2.List集合拷贝
集合拷贝和实体类对象拷贝,需要注意的点差不多,这边不做详细解释了,直接上常用到的情况代码。
实体类准备:
public class modelClass {
public static void main(String[] args) {
List<User> userList = new ArrayList<>();
List<String> list = new ArrayList<>();
CollectionUtils.addAll(list,"001","002","003");
for (int i = 1; i < 3; i++) {
User user = User.builder()
.id(i+"00")
.account("A00"+i)
.name("张三")
.password("123456")
.mobile("12345678912")
.build();
userList.add(user);
}
System.out.println(userList);
//无需特殊处理
List<UserDTO> users1 = BeanUtil.copyToList(userList, UserDTO.class);
System.out.println(users1);
//配置拷贝选项
Map<String, String> mapping = new HashMap<>(1);
// key 为源字段,value为目标字段
mapping.put("name", "userName");
List<UserDTO> users2 = BeanUtil.copyToList(userList, UserDTO.class,
CopyOptions.create()
.setFieldMapping(mapping)
.setIgnoreCase(true)
.setIgnoreProperties("account"));
System.out.println(users2);
}
}
输出: