简记JavaBean对象之间复制copy的问题
使用工具
1.IDEA
2.jdk自带的Jconsole.exe
3.源码地址:https://gitee.com/JiuLongBingShi/bmall-parent/tree/master/bmall-demo/bmall-copy
一、四种copy工具类的使用
1.org.apache.commons.beanutils.PropertyUtils.copyProperties(toBean, frombean);
2.org.apache.commons.beanutils.BeanUtils.copyProperties(toBean, frombean);
3.org.springframework.beans.BeanUtils.copyProperties(frombean, toBean);
4.org.springframework.cglib.beans.BeanCopier.create(TestDTO.class, TestEntity.class, false).copy(frombean, toBean, null);
测试结果如下:
10次测试:
1000次测试:
100W次测试:
1000W次测试:
1.数据量小的时候(10条):效率为:PropertyUtils.copyProperties>springframework.BeanUtils>BeanCopier>apache.BeanUtils.copyProperties
2.数据量大的时候(1000W条).效率为:BeanCopier>springframework.BeanUtils>PropertyUtils.copyProperties >apache.BeanUtils.copyProperties
一定要使用copy工具时,可以得出几个结论:
1.数据量小时,用PropertyUtils.copyProperties或者springframework.BeanUtils比较合适.
2.数据量大时,用cglib.BeanCopier或者springframework.BeanUtils比较合适.
二、BeanCopier和原始的SetGet对比
1.数据量小的时候(10条):BeanCopier9毫秒大于SetGet0毫秒
2.数据量小的时候(1000条):BeanCopier9毫秒大于SetGet0毫秒
3.数据量大的时候(1000W条).BeanCopier713毫秒大于SetGet83毫秒
4.数据量大的时候(5000W条).BeanCopier3467毫秒大于SetGet228毫秒
5.数据量大的时候(1亿条).BeanCopier6659毫秒大于SetGet559毫秒
可以得出几个结论:
1..当数据量小时,SetGet完胜BeanCopier.
2.当数据量在千万级的时候,两张的耗时增长都是近乎倍数,故此时SetGet和BeanCopier的效果差不多.
3.所以,能用getset的时候尽量使用getset....(偷懒是会消耗性能为代价的.说的就是我自己,哈哈哈!!!)