目录
3. cn.hutool.core.bean.BeanUtil 包下 copyProperties 方法
4. org.springframework.beans.BeanUtils 包下 copyProperties 方法
1. 摘要
在开发过程中,我们通常会在 Entity,model,DTO,VO,POJO,Param 等众多实体类之间进行互相转换,传统的做法就是 new 一个新的要转化的对象然后一个属性一个属性的 set 进去,如果是集合,外边再加一层循环嵌套即可,但尽管如此,步骤还是较为繁琐,而且当实体类中的属性较多时,代码还会显得特别臃肿,不美观。
实际上,我们可以使用别人现成的API来完成属性转换,开发过程中比较常用的就是 copyProperties 方法,而很多第三包jar依赖包中都含有 copyProperties 方法,下面我就挑两个比较常用的来说说它们的使用方法和使用过程中需要注意的点。
2. 传统 set 方法转化属性
如下代码,编写一个 Student 学生类和 Teacher 教师类,并且新建一个 Student 学生类对象 student 赋值,现在我们希望将 student 对象中的属性值赋值给新建的 Teacher 类对象 teacher。
传统做法就是 teacher.setXxx(student.getXxx),将student 中的属性获取出来赋值给 teacher,teacher中没有的属性是不能进行赋值的;
public class TestCopyProperties {
public static void main(String[] args) {
Student student = new Student();
student.setId(1L);
student.setName("张三");
student.setAge(18);
student.setEmail("qq.com");
System.out.println("原始对象 "+student);
Teacher teacher = new Teacher();
teacher.setId(student.getId());
teacher.setName(student.getName());
teacher.setAge(student.getAge());
teacher.setEmail(student.getEmail());
System.out.println("转化对象 "+teacher);
}
}
@Data
class Student {
private Long id;
private String name;
private int age;
private String email;
}
@Data
class Teacher {
private Long id;
private String name;
private int age;
private String email;
}
运行main方法,就可以在控制台看到转化对象 teache 中的属性和student对象中的属性一样;
3. cn.hutool.core.bean.BeanUtil 包下 copyProperties 方法
copyProperties 方法需要传递两个参数
copyProperties(希望转化成的实体类对象,原实体类对象)
众所周知,开发过程中DTO,VO,entity等众多实体类的属性不一定相同,但离不开四种情况;
第一种:转化类属性多于被转化类属性;
第二种:转化类属性少于被转化类属性;
第三种:转化类属性与被转化类属性存在交叉;
第四种:转化类属性等于余被转化类属性;
但还有一种情况,我们姑且归类于第五种,如果被转化对象为null,
下面主要讨论前三种情况,第四种不用说各位也知道,肯定是全部转化过去;
3.1 转化对象属性多于原始对象属性
我们将上述Student学生类对象中 email 属性去除,此时 Teacher 类比 Student 类多一个 email 属性,再进行转化
此外这里多提一句,代码中方法的前缀 "cn.hutool.core.bean" 是可以删掉的,我这里是为了方便各位小伙伴知道调用的是哪个包中的方法
public class TestCopyProperties {
public static void main(String[] args) {
Student student = new Student();
student.setId(1L);
student.setName("张三");
student.setAge(18);
System.out.println("原始对象 "+student);
// cn.hutool.core.bean.BeanUtil 包下 copyProperties 方法
Teacher teacher2 = new Teacher();
// 前缀cn.hutool.core.bean 可以删除
// 直接写成 BeanUtil.copyProperties(student, teacher2);
cn.hutool.core.bean.BeanUtil.copyProperties(student, teacher2);
System.out.println("由 cn.hutool.core.bean.BeanUtil.copyProperties转化对象 "+teacher2);
}
}
}
@Data
class Student {
private Long id;
private String name;
private int age;
}
@Data
class Teacher {
private Long id;
private String name;
private int age;
private String email;
}
可以看到转化后的 teacher 对象邮箱属性为null,说明不存在的属性值默认赋值为null;
3.2 转化对象属性少于原始对象属性
与刚才相反,现在将Teacher类中的 email 属性删除,将Student 类中的 email 属性加上去,再进行转化;
public class TestCopyProperties {
public static void main(String[] args) {
Student student = new Student();
student.setId(1L);
student.setName("张三");
student.setAge(18);
student.setEmail("qq.com");
System.out.println("原始对象 "+student);
// cn.hutool.core.bean.BeanUtil 包下 copyProperties 方法
Teacher teacher2 = new Teacher();
cn.hutool.core.bean.BeanUtil.copyProperties(student, teacher2);
System.out.println("由 cn.hutool.core.bean.BeanUtil.copyProperties转化对象 "+teacher2);
}
}
@Data
class Student {
private Long id;
private String name;
private int age;
private String email;
}
@Data
class Teacher {
private Long id;
private String name;
private int age;
}
可以看到,转化后 students 对象的 email 属性丢失,因为teacher对象中没有 email 属性来接收;
3.3 转化对象属性与原始对象属性存在差
现在在Teacher类中添加sex属性,在Student类中添加email属性,两个类互相存在差属性,
public class TestCopyProperties {
public static void main(String[] args) {
Student student = new Student();
student.setId(1L);
student.setName("张三");
student.setAge(18);
student.setEmail("qq.com");
System.out.println("原始对象 "+student);
// cn.hutool.core.bean.BeanUtil 包下 copyProperties 方法
Teacher teacher2 = new Teacher();
cn.hutool.core.bean.BeanUtil.copyProperties(student, teacher2);
System.out.println("由 cn.hutool.core.bean.BeanUtil.copyProperties转化对象 "+teacher2);
}
}
@Data
class Student {
private Long id;
private String name;
private int age;
private String email;
}
@Data
class Teacher {
private Long id;
private String name;
private int age;
private String sex;
}
运行方法,可以看到 sex 属性为 null ,因为student对象中没有此属性,而student中的 email 属性也无法赋值给teacher,因为techer没有这个属性值来接收
4. org.springframework.beans.BeanUtils 包下 copyProperties 方法
copyProperties 方法需要传递两个参数
copyProperties(希望转化成的实体类对象,原实体类对象)
这里我就不再详细复制代码了,直接粘贴运行代码截图供各位观看;
有一点要注意,此包下 copyProperties 方法可能会产生异常,需要抛出或 try...catch... 环绕;
4.1 转化对象属性多于原始对象属性
@Data
class Student {
private Long id;
private String name;
private int age;
private String email;
}
@Data
class Teacher {
private Long id;
private String name;
private int age;
}
运行方法,可以看到转化后 student 对象email属性丢失,的 teacher 对象;
4.2 转化对象属性少于原始对象属性
@Data
class Student {
private Long id;
private String name;
private int age;
private String email;
}
@Data
class Teacher {
private Long id;
private String name;
private int age;
}
4.3 转化对象属性与原始对象属性存在差
@Data
class Student {
private Long id;
private String name;
private int age;
private String email;
}
@Data
class Teacher {
private Long id;
private String name;
private int age;
private String sex;
}
5. 小结
综合上述代码举例分析,我们可以得到如下结论;
结论一:cn.hutool.core.bean.BeanUtil 包下的 copyProperties和 org.springframework.beans.BeanUtils 包下的 copyProperties 方法 均可以达到复制转换实体属性的目的;
结论二:当转化对象属性多于被转化对象属性时,多余出来的属性会被默认赋予 null 值;
结论三:当转化对象属性少于被转化对象属性时,被转化属性中多余的属性值会在转化后丢失无法传递到转化对象中;
结论四:当转化对象与被转化对象各自存在差值时,结果与结论二相同;