一、背景
- 在平时的Java开发过程中,经常会Copy 对象,如果用Setter去一个一个属性的复制,会产生很多业务无关的冗余代码(但是执行效率最高),所以在这里选择Bean Copy工具,提高开发效率:
二、测试过程
Copy 1000w条数据,输出的执行时间 单位是毫秒
原生cglib beanCopier.copy(from, to, null);
private static BeanCopier beanCopier = BeanCopier.create(User.class, User.class, false);
public static void main(String[] args) {
for (int j = 0; j < 5; j++) {
long start = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
User from = new User();
from.setId(0);
from.setName("");
from.setPassword("");
from.setEmail("");
User to = new User();
beanCopier.copy(from, to, null);
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
测试结果:49、14、6、8、7 ms
cn.hutool.core.bean.BeanUtil:
BeanUtil.copyProperties(from, to, true);
public static void main(String[] args) {
for (int j = 0; j < 5; j++) {
long start = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
UserPO from = new UserPO();
from.setId(0);
from.setName("");
from.setAddress("");
UserPO to = new UserPO();
BeanUtil.copyProperties(from, to, true);
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
测试结果:10063、7935、8183、8110、8144 ms