正经学徒,佛系记录,不搞事情
直接使用commons-beanutils实现对象拷贝,引入pom
commons-beanutils
commons-beanutils
1.9.3
创建工具类:BeanUtils
public class BeanUtils {
/**
* @param orig 源对象
* @param dest 目标对象
*/
public static void copyProperties(final Object orig,final Object dest){
try{
org.apache.commons.beanutils.BeanUtils.copyProperties(dest, orig);
}catch (Exception e){
throw new RuntimeException(e.getMessage());
}
}
/**
* @Description:拷贝list元素对象,将origs中的元素信息,拷贝覆盖至dests中
* @param origs 源list对象
* @param dests 目标list对象
* @param origsElementTpe 源list元素类型对象
* @param destElementTpe 目标list元素类型对象
* @param 源list元素类型
* @param 目标list元素类型
*/
public static void copyProperties(final List origs, final List dests, Class origsElementTpe, Class destElementTpe){
if(origs==null||dests==null){
return ;
}
if(dests.size()!=0){
//防止目标对象被覆盖,要求必须长度为零
throw new RuntimeException("目标对象存在值");
}
try{
for (T1 orig: origs) {
T2 t = destElementTpe.newInstance();
dests.add(t);
copyProperties(orig,t);
}
}catch (Exception e){
throw new RuntimeException(e.getMessage());
}
}
}
注:抛出异常部分可根据实际项目需要,抛出自定义异常。
创建两个实体类,实体类只相差一个属性,用于测试
BeanOne.java
private String id;
private String name;
private String beanOne;
BeanTwo.java
private String id;
private String name;
private String beanTwo;
BeanOne orig = new BeanOne("123","张三","one");
//直接赋值
BeanOne dest = orig;
//修改值
dest.setId("456");
dest.setName("李四");
System.out.println("赋值后的原始值:"+orig.toString());
System.out.println("赋值后新对象值:"+dest.toString());
结果:直接拷贝对象时,是地址拷贝,orig与dest指向同一个地址,导致修改dest会影响到orig
BeanOne orig = new BeanOne("123","张三","one");
//深度克隆
BeanOne dest = new BeanOne();
BeanUtils.copyProperties(orig,dest);
dest.setId("456");
dest.setName("李四");
System.out.println("赋值后的原始值:"+orig.toString());
System.out.println("赋值后新对象值:"+dest.toString());
结果:修改dest不会影响到orig的值
BeanOne orig = new BeanOne("123","张三","one");
//深度克隆
BeanTwo dest = new BeanTwo();
BeanUtils.copyProperties(orig,dest);
dest.setName("李四");
System.out.println("赋值后的原始值:"+orig.toString());
System.out.println("赋值后新对象值:"+dest.toString());
结果:不同类型的对象之间的拷贝,则相同属性会拷贝,不同属性不会拷贝
注:追溯org.apache.commons.beanutils.BeanUtils源码,克制对象之间的拷贝是通过属性名的而与类型无关,未避免异常,两个不同类型的对象之间的拷贝,尽可能保证相同属性名的类型也保持一致。
List origs = new ArrayList<>();
origs.add(new BeanOne("123","张三","one"));
origs.add(new BeanOne("456","李四","one1"));
//深度克隆
List dests = new ArrayList<>();
BeanUtils.copyProperties(origs,dests,BeanOne.class,BeanTwo.class);
for(BeanTwo temp : dests){
temp.setName("都是two");
}
System.out.println("赋值后的原始值:"+origs.toString());
System.out.println("赋值后新对象值:"+dests.toString());
结果:拷贝成功,且不影响原始值