mapstruct中使用lombok@Builder的坑

在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;
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值