copyPropertie(Object source, Object target) source 表示需要获得资源的bean对象 target 表示现有的bean对象
1.新建两个实体类UserVo,UserDTO
package com.fanfan.shiroadmin.modules.sys.entity;
import java.util.Date;
public class UserDTO {
private String userName;
private Integer age;
private Date date;
public UserDTO() {
}
public UserDTO(String userName, Integer age, Date date) {
this.userName = userName;
this.age = age;
this.date = date;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "UserDTO{" +
"userName='" + userName + '\'' +
", age=" + age +
", date=" + date +
'}';
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.fanfan.shiroadmin.modules.sys.entity;
public class UserVo {
private String userName;
private int age;
public UserVo(String userName, int age) {
this.userName = userName;
this.age = age;
}
@Override
public String toString() {
return "UserVo{" +
"userName='" + userName + '\'' +
", age=" + age +
'}';
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2.开始测试
2.1 使用org.springframework.beans.BeanUtils的copyProperties()方法
@Test
public void VOToDTOTest01(){
log.info("测试开始。。。");
for (int i = 0; i < 1000000; i++ ){
UserVo userVo = new UserVo("张三", 18);
UserDTO userDTO = new UserDTO();
BeanUtils.copyProperties(userDTO,userVo);
}
log.info("测试结束");
}
执行结果: 原因: org.springframework.beans.BeanUtils的copyProperties()方法好像不支持拆箱操作,只能进行同等类型的copy操作
2.2改用org.apache.commons.beanutils.BeanUtils的copyProperties()方法
@Test
public void VOToDTOTest02(){
log.info("测试开始。。。");
for (int i = 0; i < 1000000; i++ ){
UserVo userVo = new UserVo("张三", 18);
UserDTO userDTO = new UserDTO();
try {
BeanUtils.copyProperties(userDTO,userVo);
} catch (IllegalAccessException | InvocationTargetException e) {
log.error("userDTO值为{},错误提示为{}",userDTO.toString(),e.toString());
e.printStackTrace();
}
}
log.info("测试结束");
}
测试结果: 执行100万次copy操作用来2秒左右的时间 此时想测试一下使用org.springframework.beans.BeanUtils的copyProperties()执行100万条copy需要多少时间
新建UserBo类
package com.fanfan.shiroadmin.modules.sys.entity;
public class UserBo {
private String userName;
private int age;
public UserBo(){
}
public UserBo(String userName, int age) {
this.userName = userName;
this.age = age;
}
@Override
public String toString() {
return "UserVo{" +
"userName='" + userName + '\'' +
", age=" + age +
'}';
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
进行单元测试
@Test
public void VOToDTOTest04(){
log.info("测试开始。。。");
for (int i = 0; i < 1000000; i++ ){
UserVo userVo = new UserVo("张三", 18);
UserBo userBo= new UserBo();
try {
BeanUtils.copyProperties(userBo,userVo);
} catch (IllegalAccessException | InvocationTargetException e) {
log.error("userDTO值为{},错误提示为{}",userBo.toString(),e.toString());
e.printStackTrace();
}
}
log.info("测试结束");
}
执行结果
@Test
public void VOToDTOTest05(){
log.info("测试开始。。。");
for (int i = 0; i < 1000000; i++ ){
UserVo userVo = new UserVo("张三", 18);
UserBo userBo = new UserBo();
BeanUtils.copyProperties(userBo,userVo);
}
log.info("测试结束");
}
测试结果: 只用了不到1s 得出结论: 在进行copy javaBean对象时 如果copy前后两个bean类型一致,使用springframework提供的copyProperties更快,不一致且对copy效率不是很看重的情况使用apache的copyProperties方法更合适。 而且springframework提供的copyProperties默认进行一次处理,而apache的copyProperties有强制的异常处理机制
使用get,set方法赋值
@Test
public void VOToDTOTest03(){
log.info("测试开始。。。");
for (int i = 0; i < 1000000; i++ ){
UserVo userVo = new UserVo("张三", 18);
UserDTO userDTO = new UserDTO();
userDTO.setUserName(userVo.getUserName());
userDTO.setAge(userVo.getAge());
}
log.info("测试结束");
}
测试结果: 得出结论:使用get,set方法赋值效率最高,但是在实际开发中会出现很多的臃肿的代码块
4.提出疑问,寻求大佬解答
在alibaba代码规范中提出了DO,DTO,BO,AO,VO等概念,在代码运行的不同阶段,pojo之间是如何转换的?使用怎样的方式比较合适?不是很理解,希望有大佬的解答
|