平时我们在业务开发的时候经常会遇到:操作数据库是通过entity(实体类)、Dto,但是返回给前端Vo,这个时候很有可能查出的字段是 entity/dto 多于 vo 的,这时我们就需要将entity/dto转化为vo,一般有两种方式:
第一种方式是:将vo所需字段一一判空,非空的set到新new的对象中(此处省略demo)
第二种方式是:通过BeanUtils.copyProperties()来实现:
实体类qian
@Data
public class qian {
private String a;
private Integer b;
private String c;
}
实体类hou
@Data
public class hou {
private String b;
private Integer d;//qian类中没有的属性
private String c;
}
测试类:
public static void main(String[] args) {
qian qian = new qian();
qian.setA("A");
qian.setB(1);
qian.setC("C");
System.out.println("qian:"+qian);
hou hou = new hou();
BeanUtils.copyProperties(qian,hou);
System.out.println("hou:"+hou);
}
输出:
qian:qian(a=A, b=1, c=C)
hou:hou(b=1, d=null, c=C)
看起来是不是很“完美”?
但是这里有一个值得注意的点,BeanUtils.copyProperties()它是“浅拷贝”,什么是“浅拷贝”呢?让我们来再看个例子:
我们修改一下代码,分别在qian
、hou
添加一个practiceTeacher实体属性:
修改后qian
:
@Data
public class qian {
private String a;
private Integer b;
private String c;
public PracticeTeacher teacher;
}
修改后hou
:
@Data
public class hou {
private Integer b;
private Integer d;
private String c;
public PracticeTeacher teacher;
}
修改后测试类:
PracticeTeacher practiceTeacher = new PracticeTeacher();
practiceTeacher.setUserId(123);
qian qian = new qian();
qian.setA("A");
qian.setB(1);
qian.setC("C");
qian.setTeacher(practiceTeacher);
System.out.println("qian:"+qian);
hou hou = new hou();
BeanUtils.copyProperties(qian, hou);
System.out.println("hou:"+hou);
practiceTeacher.setUserId(456);
qian.setTeacher(practiceTeacher);
qian.setC("newC");
System.out.println("修改后qian:"+qian);
System.out.println("修改后hou:"+hou);
输出:
qian:qian(a=A, b=1, c=C, teacher=PracticeTeacher(id=null, userId=123, nickName=null, teachSub=null))
hou:hou(b=1, d=null, teacher=PracticeTeacher(id=null, userId=123, nickName=null, teachSub=null))
修改后qian:qian(a=A, b=1, c=newC, teacher=PracticeTeacher(id=null, userId=456, nickName=null, teachSub=null))
修改后hou:hou(b=1, d=null, c=C, teacher=PracticeTeacher(id=null, userId=456, nickName=null, teachSub=null))
小名在新的测试类第12行修改了qian
中practiceTeacher的UserId为456,然而我们并没有重新通过BeanUtils.copyProperties()方法进行拷贝对象,但是修改后hou
中userId同修改后qian
也修改为了456,这就说明copyProperties是浅拷贝。
此时,我们来看下浅拷贝和深拷贝的区别:
不知道细心的小伙伴看到这里,是不是发现那里不对劲?代码中第14行小名重新set了类行为String的C属性,但是在修改后qian
中 c=newC,而修改后hou
中 c=C,这是为什么呢?String并不属于基本数据类型啊?为什么它没有改变呢?
这是因为 String是 final 修饰的类,所以不会被修改。
接下来,介绍一种小名用过的一种深拷贝的方法:
Jackson序列化,其实原理很简单:先序列化对象,然后再反系列化为新的对象:
PracticeTeacher practiceTeacher = new PracticeTeacher();
practiceTeacher.setUserId(123);
qian qian = new qian();
qian.setA("A");
qian.setB(1);
qian.setC("C");
qian.setTeacher(practiceTeacher);
System.out.println("qian:"+qian);
hou hou = new hou();
BeanUtils.copyProperties(qian, hou);
System.out.println("hou:"+hou);
// Jackson深拷贝
ObjectMapper objectMapper = new ObjectMapper();
hou deepCopy = objectMapper.readValue(objectMapper.writeValueAsString(hou), hou.class);
System.out.println("[deepCopy]:"+deepCopy);
practiceTeacher.setUserId(456);
qian.setTeacher(practiceTeacher);
qian.setC("newC");
System.out.println("修改后qian:"+qian);
System.out.println("修改后hou:"+hou);
System.out.println("[修改后deepCopy]:"+deepCopy);
输出结果:
qian:qian(a=A, b=1, c=C, teacher=PracticeTeacher(id=null, userId=123, nickName=null, teachSub=null))
hou:hou(b=1, d=null, c=C, teacher=PracticeTeacher(id=null, userId=123, nickName=null, teachSub=null))
[deepCopy]:hou(b=1, d=null, c=C, teacher=PracticeTeacher(id=null, userId=123, nickName=null, teachSub=null))
修改后qian:qian(a=A, b=1, c=newC, teacher=PracticeTeacher(id=null, userId=456, nickName=null, teachSub=null))
修改后hou:hou(b=1, d=null, c=C, teacher=PracticeTeacher(id=null, userId=456, nickName=null, teachSub=null))
[修改后deepCopy]:hou(b=1, d=null, c=C, teacher=PracticeTeacher(id=null, userId=123, nickName=null, teachSub=null))
我们可以看到:deepCopy在修改前后,userId都是123,不会像hou
那样,随着拷贝对象的变化而变化了~
如若您在文章中发现任何错误的地方,小名希望您可以在评论区给予批评指正🤝 如果觉得小名的文章帮助到了您,请关注小名的专栏【日常记错】,支持一下小名😄,给小名的文章点赞👍、评论✍、收藏🤞谢谢大家啦~♥♥♥