无意中见同事在比较BeanCopier的效率,MapStruct的使用者很牛皮的说我的效率是你的XX倍,今天认识了一下MapStrut,毫无疑问反射的效率绝对输给setter/getter
引入
org.mapstruct mapstruct-jdk8 ${org.mapstruct.version} org.mapstruct mapstruct-processor ${org.mapstruct.version} provided
maven编译更新成1.8结合lombok
,同时更新maven-compiler-plugin
org.apache.maven.plugins maven-compiler-plugin 3.6.2
加-Amapstruct.defaultComponentModel=spring
编译配置的目的是指定mapstruct编译生成实现类的时候支持spring的扫描
编译
忽略具体bean,copier如下
@Mapperpublic interface OrderFundsLiteMapper { WjsOrderFundsLiteToValidBean toWjsOrderFundsLiteToValidBean(OrderFundsLiteDO orderFundsLiteDO);}
maven编译,实现类如下
@Generated( value = "org.mapstruct.ap.MappingProcessor", date = "2018-04-12T10:58:53+0800", comments = "version: 1.2.0.Final, compiler: javac, environment: Java 1.8.0_111 (Oracle Corporation)")@Componentpublic class OrderFundsLiteMapperImpl implements OrderFundsLiteMapper { @Override public WjsOrderFundsLiteToValidBean toWjsOrderFundsLiteToValidBean(OrderFundsLiteDO orderFundsLiteDO) { if ( orderFundsLiteDO == null ) { return null; } WjsOrderFundsLiteToValidBean wjsOrderFundsLiteToValidBean = new WjsOrderFundsLiteToValidBean(); wjsOrderFundsLiteToValidBean.setOrderFundsId( orderFundsLiteDO.getOrderFundsId() ); wjsOrderFundsLiteToValidBean.setAgencyCode( orderFundsLiteDO.getAgencyCode() ); wjsOrderFundsLiteToValidBean.setOrderFundsAmt( orderFundsLiteDO.getOrderFundsAmt() ); wjsOrderFundsLiteToValidBean.setGmtContractUpdate( orderFundsLiteDO.getGmtContractUpdate() ); return wjsOrderFundsLiteToValidBean; }}
可见MapStruct在编译期生成实现类,同时作为spring的@Component
,可以直接注入使用。
运行
WjsOrderFundsLiteToValidBean(orderFundsId=1, agencyCode=twen, orderFundsAmt=1, gmtContractUpdate=Thu Apr 12 11:05:13 CST 2018)
总结
MapStruct在编译时,自动生成具体的setter/getter,减少了代码量,同时避免反射带来的效率牺牲。
后
具体学习文档 参见 MapStruct 1.2.0.Final参考指南