java之对象拷贝(A对象中的数据拷贝到B对象)

使用BeanUtils.copyProperties(source,target);方法

(BeanUtils.copyProperties()方法是浅拷贝)


//我要把
PrjPlanDetailEntity 对象的数据拷贝到 PrjBdgDetailEntity,只要他们有相同的字段,数据就会拷贝过去

PrjPlanDetailEntity p1 = new PrjPlanDetailEntity();
p1.setPlanStartDate(new Date());
p1.setPlanEndDate(DateUtils.parse("2020-12-16", "yyyy-MM-dd"));

PrjBdgDetailEntity p2 = new PrjBdgDetailEntity();
BeanUtils.copyProperties(p2, p1);
System.out.println(p2.getPlanEndDate());


p2对象的planEndDate字段打印出来数据说明拷贝成功了
原文地址:https://www.cnblogs.com/spll/p/14147781.html