java复制原对象有值的属性到新对象同时不改变原对象的固有的值(元素相同)
前提
说明:一般在一些业务字段特别多的情况并且由多个相同属性时,为了避免冗余的get和set方法,降低代码长度和阅读代码的难度。才会出现属性复制、浅拷贝、深拷贝的一些工具类。
首先要明确一点 BeanUtils.copyProperties() 是简单的属性复制,不是拷贝。其主要目的是利用反射机制对JavaBean的属性进行处理。
问题
但是使用这种属性赋值有个缺点,大家见下图
我这里使用的是org.springframework.beans.BeanUtils
package org.example.test; public class Student { private String name; private String sex; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + '}'; } }
package org.example.test; import org.springframework.beans.BeanUtils; public class StudentDemo { public static void main(String[] args) { Student student1 = new Student(); student1.setName("张三"); student1.setAge(18); System.out.println("拷贝前 = " + student1); Student student2 = new Student(); BeanUtils.copyProperties(student2, student1); System.out.println("拷贝后 = " + student1); } }
# 输出结果: 拷贝前 = Student{name='张三', sex='null', age=18} 拷贝后 = Student{name='null', sex='null', age=null} Process finished with exit code 0
一个很明显的问题:student1 本身name和age属性已经带了值,我使用一个新student2去接收student1的属性值却都是null值。因为BeanUtils.copyProperties()是直接覆盖属性而不是在内部对null做判断处理。
需求
那么我现在有这么一个情况我从页面传递过来一个数据实体,现在我要在后台处理数据实体的同时给该实体添加一些其他属性。
解决方案
1,导包
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.0.7</version> </dependency>
2,使用
代码说话
package org.example.test; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.copier.CopyOptions; public class StudentDemo { public static void main(String[] args) { Student student1 = new Student(); student1.setName("张三"); student1.setAge(18); System.out.println("拷贝前 = " + student1); Student student2 = new Student(); BeanUtil.copyProperties(student2, student1, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true)); System.out.println("拷贝后 = " + student1); } }
# 输出结果: 拷贝前 = Student{name='张三', sex='null', age=18} 拷贝后 = Student{name='张三', sex='null', age=18} Process finished with exit code 0
那么我给student2再放一个属性值呢
package org.example.test; import cn.hutool.core.bean.BeanUtil; public class StudentDemo3 { public static void main(String[] args) { Student student1 = new Student(); student1.setName("张三"); student1.setAge(18); System.out.println("拷贝前 = " + student1); Student student2 = new Student(); student2.setSex("男"); BeanUtil.copyProperties(student2, student1); System.out.println("拷贝后 = " + student1); } }
# 输出结果 拷贝前 = Student{name='张三', sex='null', age=18} 拷贝后 = Student{name='null', sex='男', age=null} Process finished with exit code 0
好了,完美满足我们的要求,我不仅要原来对象的属性我还要给新的对象放其他的属性。同时你要保证我新的对象原属性中有值的你还得给我带过来。
核心代码
// 将第一个参数的元素值赋值给第二个参数,同时不改变第二个引用对象 原有的值 // setIgnoreNullValue(true)为忽略null值 //setIgnoreError(true) 忽略字段注入错误 BeanUtil.copyProperties(oldObj, newObj, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
原文链接:https://blog.csdn.net/weixin_44835297/article/details/119646806
标签:
java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)