作者:小傅哥
博客:https://bugstack.cn
原文:https://mp.weixin.qq.com/s/Xq7oQg7dYESMYxHVnxX8Dw
积淀、分享、成长,让本人和别人都能有所播种!
一、前言
为哈么,你的代码也就仅仅是能用而已?
没有技术深度、短缺常识储备、匮乏教训积攒的前提下,怎么写代码?百度呀,遇到问题这搜一点,那查一块,不论它是什么原理还是适宜哪种场景,先粘贴到本人的工程里,看,能跑了,能跑就行。那这样的代码也就仅仅是能用水平的交付,基本没有肯定的质量保证,也更别提数据结构、算法逻辑和设计模式了,那看的编程材料刷的LeetCode,全歇菜了。
当你感觉看了很多材料又不会用的时候,会说什么,真卷,都学到这样了
。但其实我并不觉对技术的深度开掘、梳理全套的常识体系,一点点耕耘一点点播种是在卷。反而把看技术视频当成看电影一样轻松,不写案例就认为书看会了的爽,没有意义的短少脑力思考机械式膂力反复,才是卷,甚至很卷。
就像让你用一个属性拷贝工具,把vo转成dto
,你用了哪呢,是 Apache 的还是 Spring 的,还是其余的什么,哪个效率最高?接下来咱们来用数据验证下,并提供出各种案例的应用比照
二、性能测试比照
在 Java 系统工程开发过程中,都会有各个层之间的对象转换,比方 VO、DTO、PO、VO 等,而如果都是手动get、set
又太浪费时间,还可能操作谬误,所以抉择一个自动化工具会更加不便。
目前我整顿出,用于对象属性转换有12种,包含:一般的getset、json2Json、Apache属性拷贝、Spring属性拷贝、bean-mapping、bean-mapping-asm、BeanCopier、Orika、Dozer、ModelMapper、JMapper、MapStruct 接下来咱们别离测试这11种属性转换操作别离在一百次
、一千次
、一万次
、十万次
、一百万次
时候的性能工夫比照。
BeanUtils.copyProperties
是大家代码里最常呈现的工具类,但只有你不把它用错成Apache
包下的,而是应用 Spring 提供的,就根本还不会对性能造成多大影响。- 但如果说性能更好,可代替手动
get、set
的,还是MapStruct
更好用,因为它自身就是在编译期生成get、set
代码,和咱们写get、set
一样。 - 其余一些组件包次要基于
AOP
、ASM
、CGlib
,的技术手段实现的,所以也会有相应的性能损耗。
三、12种转换案例
源码:https://github.com/fuzhengwei/guide-vo2dto
形容:在案例工程下创立 interfaces.assembler 包,定义 IAssembler<SOURCE, TARGET>#sourceToTarget(SOURCE var)
接口,提供不同形式的对象转换操作类实现,学习的过程中能够间接下载运行调试。
1. get\set
@Componentpublic class GetSetAssembler implements IAssembler<UserVO, UserDTO> { @Override public UserDTO sourceToTarget(UserVO var) { UserDTO userDTO = new UserDTO(); userDTO.setUserId(var.getUserId()); userDTO.setUserNickName(var.getUserNickName()); userDTO.setCreateTime(var.getCreateTime()); return userDTO; }}
- 举荐:★★★☆☆
- 性能:★★★★★
- 伎俩:手写
- 点评:其实这种形式也是日常应用的最多的,性能必定是杠杠的,就是操作起来有点麻烦。尤其是一大堆属性的 VO 对象转换为 DTO 对象时候。但其实也有一些快捷的操作形式,比方你能够通过 Shift+Alt 选中所有属性,Shift+Tab 归并到一列,接下来在应用 Alt 选中这一列,批量操作粘贴
userDTO.set
以及快捷键大写属性首字母,最初切换到结尾补充括号和分号,最终格式化一下就搞定了。
2. json2Json
@Componentpublic class Json2JsonAssembler implements IAssembler<UserVO, UserDTO> { @Override public UserDTO sourceToTarget(UserVO var) { String strJson = JSON.toJSONString(var); return JSON.parseObject(strJson, UserDTO.class); }}
- 举荐:☆☆☆☆☆
- 性能:★☆☆☆☆
- 伎俩:把对象转JSON串,再把JSON转另外一个对象
- 点评:这么写多半有点烧!
3. Apache copyProperties
@Componentpublic class ApacheCopyPropertiesAssembler implements IAssembler<UserVO, UserDTO> { @Override public UserDTO sourceToTarget(UserVO var) { UserDTO userDTO = new UserDTO(); try { BeanUtils.copyProperties(userDTO, var); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } return userDTO; }}
- 举荐:☆☆☆☆☆
- 性能:★☆☆☆☆
- 伎俩:Introspector 机制获取到类的属性来进行赋值操作
- 点评:有坑,兼容性交差,不倡议应用
4. Spring copyProperties
@Componentpublic class SpringCopyPropertiesAssembler implements IAssembler<UserVO, UserDTO> { @Override public UserDTO sourceToTarget(UserVO var) { UserDTO userDTO = new UserDTO(); BeanUtils.copyProperties(var, userDTO); return userDTO; }}
- 举荐:★★★☆☆
- 性能:★★★★☆
- 伎俩:Introspector机制获取到类的属性来进行赋值操作
- 点评:同样是反射的属性拷贝,Spring 提供的 copyProperties 要比 Apache 好用的多,只有你不必错,根本不会有啥问题。
5. Bean Mapping
@Componentpublic class BeanMappingAssembler implements IAssembler<UserVO, UserDTO> { @Override public UserDTO sourceToTarget(UserVO var) { UserDTO userDTO = new UserDTO(); BeanUtil.copyProperties(var, userDTO); return userDTO; }}
- 举荐:★★☆☆☆
- 性能:★★★☆☆
- 伎俩:属性拷贝
- 点评:性能个别
6. Bean Mapping ASM
@Componentpublic class BeanMappingAssembler implements IAssembler<UserVO, UserDTO> { @Override public UserDTO sourceToTarget(UserVO var) { UserDTO userDTO = new UserDTO(); BeanUtil.copyProperties(var, userDTO); return userDTO; }}
- 举荐:★★★☆☆
- 性能:★★★★☆
- 伎俩:基于ASM字节码框架实现
- 点评:与一般的 Bean Mapping 相比,性能有所晋升,能够应用。
7. BeanCopier
@Componentpublic class BeanCopierAssembler implements IAssembler<UserVO, UserDTO> { @Override public UserDTO sourceToTarget(UserVO var) { UserDTO userDTO = new UserDTO(); BeanCopier beanCopier = BeanCopier.create(var.getClass(), userDTO.getClass(), false); beanCopier.copy(var, userDTO, null); return userDTO; }}
- 举荐:★★★☆☆
- 性能:★★★★☆
- 伎俩:基于CGlib字节码操作生成get、set办法
- 点评:整体性能很不错,应用也不简单,能够应用
8. Orika
@Componentpublic class OrikaAssembler implements IAssembler<UserVO, UserDTO> { /** * 结构一个MapperFactory */ private static MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); static { mapperFactory.classMap(UserDTO.class, UserVO.class) .field("userId", "userId") // 字段不统一时能够指定 .byDefault() .register(); } @Override public UserDTO sourceToTarget(UserVO var) { return mapperFactory.getMapperFacade().map(var, UserDTO.class); }}
- 官网:https://orika-mapper.github.io/orika-docs/
- 举荐:★★☆☆☆
- 性能:★★★☆☆
- 伎俩:基于字节码生成映射对象
- 点评:测试性能不是太突出,如果应用的话须要把 MapperFactory 的构建优化成 Bean 对象
9. Dozer
@Componentpublic class DozerAssembler implements IAssembler<UserVO, UserDTO> { private static DozerBeanMapper mapper = new DozerBeanMapper(); @Override public UserDTO sourceToTarget(UserVO var) { return mapper.map(var, UserDTO.class); }}
- 官网:http://dozer.sourceforge.net/documentation/gettingstarted.html
- 举荐:★☆☆☆☆
- 性能:★★☆☆☆
- 伎俩:属性映射框架,递归的形式复制对象
- 点评:性能有点差,不倡议应用
10. ModelMapper
@Componentpublic class ModelMapperAssembler implements IAssembler<UserVO, UserDTO> { private static ModelMapper modelMapper = new ModelMapper(); static { modelMapper.addMappings(new PropertyMap<UserVO, UserDTO>() { @Override protected void configure() { // 属性值不一样能够本人操作 map().setUserId(source.getUserId()); } }); } @Override public UserDTO sourceToTarget(UserVO var) { return modelMapper.map(var, UserDTO.class); }}
- 官网:http://modelmapper.org
- 举荐:★★★☆☆
- 性能:★★★☆☆
- 伎俩:基于ASM字节码实现
- 点评:转换对象数量较少时性能不错,如果同时大批量转换对象,性能有所降落
11. JMapper
JMapper<UserDTO, UserVO> jMapper = new JMapper<>(UserDTO.class, UserVO.class, new JMapperAPI() .add(JMapperAPI.mappedClass(UserDTO.class) .add(JMapperAPI.attribute("userId") .value("userId")) .add(JMapperAPI.attribute("userNickName") .value("userNickName")) .add(JMapperAPI.attribute("createTime") .value("createTime")) ));
- 官网:https://github.com/jmapper-framework/jmapper-core/wiki
- 举荐:★★★★☆
- 性能:★★★★★
- 伎俩:Elegance, high performance and robustness all in one java bean mapper
- 点评:速度真心能够,不过联合 SpringBoot 感觉有的一点点麻烦,可能姿态不对
12. MapStruct
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE, unmappedSourcePolicy = ReportingPolicy.IGNORE)public interface UserDTOMapping extends IMapping<UserVO, UserDTO> { /** 用于测试的单例 */ IMapping<UserVO, UserDTO> INSTANCE = Mappers.getMapper(UserDTOMapping.class); @Mapping(target = "userId", source = "userId") @Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss") @Override UserDTO sourceToTarget(UserVO var1); @Mapping(target = "userId", source = "userId") @Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss") @Override UserVO targetToSource(UserDTO var1);}
- 官网:https://github.com/mapstruct/mapstruct
- 举荐:★★★★★
- 性能:★★★★★
- 伎俩:间接在编译期生成对应的get、set,像手写的代码一样
- 点评:速度很快,不须要到运行期解决,联合到框架中使用方便
四、总结
- 其实对象属性转换的操作无非是基于反射、AOP、CGlib、ASM、Javassist 在编译时和运行期进行解决,再有好的思路就是在编译前生成出对应的get、set,就像手写进去的一样。
- 所以我更举荐我喜爱的 MapStruct,这货用起来还是比拟难受的,一种是来自于性能上的拓展性,易用性和兼容性。
- 无论哪种应用,都要做一下残缺的测试和验证,不要上来就复制粘贴,否则你可能早早的就把挖好坑了,当然不肯定是哪个兄弟来填坑了。
五、系列举荐
- DDD + RPC 开发分布式架构,抽奖零碎
- SpringBoot 中间件设计和开发
- 半年招聘筛选了400+份简历,通知你怎么写容易被撩!
- 数学,离一个程序员有多近?
- 13年毕业,用两年工夫从外包走进互联网大厂