mapstruct进阶用法《一》
一.mapstruct除了能自动映射字段属性,也可以定义javaBean见得规则进行映射
/** * 使用自定义的映射器BooleanStr */ @Mapper(uses ={BooleanStr.class}) public interface CustomerInterface { CustomerInterface INSTANCE= Mappers.getMapper(CustomerInterface.class); @Mappings({ @Mapping(source = "name", target = "customerName"), @Mapping(source = "isDisable", target = "disable") }) CustomerDto toCustomerDto(Customer customer); }
二.自定义映射规则BooleanStr 后,就可以把Customer的Boolean属性转换成目标bean的String类型
/** * 自定义转换规则的类 */ public class BooleanStr { public String toStr(Boolean isDisable) { if (isDisable) { return "Y"; } else { return "N"; } } public Boolean toBoolean(String str) { if (str.equals("Y")) { return true; } else { return false; } } }
@Generated( value = "org.mapstruct.ap.MappingProcessor", date = "2022-01-25T16:15:32+0800", comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_91 (Oracle Corporation)" ) public class CustomerInterfaceImpl implements CustomerInterface { private final BooleanStr booleanStr = new BooleanStr(); @Override public CustomerDto toCustomerDto(Customer customer) { if ( customer == null ) { return null; } CustomerDto customerDto = new CustomerDto(); customerDto.setDisable( booleanStr.toStr( customer.getIsDisable() ) ); customerDto.setCustomerName( customer.getName() ); customerDto.setId( customer.getId() ); return customerDto; } }
2.除了上面自定义规则外还可以把目标bean同过注入容器的方式来处理比如下面所示:
/** * 这里主要是这个componentModel 属性,它的值就是当前要使用的依赖注入的环境 * 必须要引入spring的jar包 */ @Mapper(componentModel = "spring") public interface RefundInterface { RefundInterface INSTANCE= Mappers.getMapper(RefundInterface.class); @Mappings({ @Mapping(source = "shardKey",target = "key") }) RefundVo refundTo(Refund refund); }
@Generated( value = "org.mapstruct.ap.MappingProcessor", date = "2022-01-25T16:15:32+0800", comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_91 (Oracle Corporation)" ) @Component public class RefundInterfaceImpl implements RefundInterface { @Override public RefundVo refundTo(Refund refund) { if ( refund == null ) { return null; } RefundVo refundVo = new RefundVo(); refundVo.setKey( refund.getShardKey() ); return refundVo; } }
上述可以以组件的方式注入容器,在需要的地方进行AtuoWrite使用
三.除了一,二分别制定spring容器和自定义规则 另外,还可以把自定义规则以springbean的形式注入到映射目标的实现中去,实现“自动化”无感知的的便捷。
这里还是以上面的为例
/** * 使用自定义的映射器BooleanStr */ @Mapper(uses ={BooleanStr.class},componentModel ="spring") public interface CustomerInterface { CustomerInterface INSTANCE= Mappers.getMapper(CustomerInterface.class); @Mappings({ @Mapping(source = "name", target = "customerName"), @Mapping(source = "isDisable", target = "disable") }) CustomerDto toCustomerDto(Customer customer); }
@Generated( value = "org.mapstruct.ap.MappingProcessor", date = "2022-01-25T17:08:28+0800", comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_91 (Oracle Corporation)" ) @Component public class CustomerInterfaceImpl implements CustomerInterface { @Autowired private BooleanStr booleanStr; @Override public CustomerDto toCustomerDto(Customer customer) { if ( customer == null ) { return null; } CustomerDto customerDto = new CustomerDto(); customerDto.setDisable( booleanStr.toStr( customer.getIsDisable() ) ); customerDto.setCustomerName( customer.getName() ); customerDto.setId( customer.getId() ); return customerDto; } }
/** * 自定义转换规则的类 * 也可以根据业务自己实现去注入,原理一样的这里不再赘述 */ @Component public class BooleanStr { public String toStr(Boolean isDisable) { if (isDisable) { return "Y"; } else { return "N"; } } public Boolean toBoolean(String str) { if (str.equals("Y")) { return true; } else { return false; } } }
总结: