在POJO类上使用了lombok@Builder注解,所以导致mapstruct编译代码使用build模式丢失父类属性
OrderRateBillDO父类具有gmtCreate等属性但是在编译的代码中没有进行赋值
@Override
public OrderRateBillDTO toDTO(OrderRateBillDO orderRateBillDO) {
if ( orderRateBillDO == null ) {
return null;
}
OrderRateBillDTOBuilder orderRateBillDTO = OrderRateBillDTO.builder();
orderRateBillDTO.bizIdOrderRateBill( orderRateBillDO.getBizIdOrderRateBill() );
orderRateBillDTO.orderNo( orderRateBillDO.getOrderNo() );
orderRateBillDTO.orderType( orderRateBillDO.getOrderType() );
return orderRateBillDTO.build();
}
Mapper注解里面有个builder属性
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface Mapper {
/**
* The information that should be used for the builder mappings. This can be used to define custom build methods
* for the builder strategy that one uses.
* If no builder is defined the builder given via {@link MapperConfig#builder()} will be applied.
* <p>
* NOTE: In case no builder is defined here, in {@link BeanMapping} or {@link MapperConfig} and there is a single
* build method, then that method would be used.
* <p>
* If the builder is defined and there is a single method that does not match the name of the build method then
* a compile error will occur
*
* @return the builder information
* @since 1.3
*/
Builder builder() default @Builder;
}
点开 @Builder如下,这里有个小坑,注意是1.3.1.Final版本以及更高才有disableBuilder属性,意思很明显,这就是是否开始构建者模式的开关,默认开启
在mapper注解上使用
@Mapper(builder = @Builder(disableBuilder = true))
public abstract class RateEventRecordConverter {
}
再看看编译后的代码,已经可以使用父类的属性
@Override
public OrderRateBillDTO toDTO(OrderRateBillDO orderRateBillDO) {
if ( orderRateBillDO == null ) {
return null;
}
OrderRateBillDTO orderRateBillDTO = new OrderRateBillDTO();
orderRateBillDTO.setId( orderRateBillDO.getId() );
orderRateBillDTO.setGmtCreate( orderRateBillDO.getGmtCreate() );
orderRateBillDTO.setCreator( orderRateBillDO.getCreator() );
orderRateBillDTO.setGmtModified( orderRateBillDO.getGmtModified() );
orderRateBillDTO.setModifier( orderRateBillDO.getModifier() );
orderRateBillDTO.setDeleted( orderRateBillDO.getDeleted() );
orderRateBillDTO.setBizIdOrderRateBill( orderRateBillDO.getBizIdOrderRateBill() );
orderRateBillDTO.setOrderNo( orderRateBillDO.getOrderNo() );
orderRateBillDTO.setOrderType( orderRateBillDO.getOrderType() );
return orderRateBillDTO;
}