Mapstruct中使用lombok@Builder的坑

2020-08-31  本文已影响0人  从入门到脱发

一丶先介绍项目背景

使用了lombok,Mapstruct,validator简化代码三剑客,使用起来是很流畅,因为一位同事在POJO类上使用了lombok@Builder注解,所以导致mapstruct编译代码使用build模式丢失父类属性

二丶产生大坑

mapstruct编译生成的代码如下,创建者模式,使用的是lombok的模板,但是这会产生一个问题,是没有办法创建父类属性

    @Override
    public SystemRole convertSystemRole(SystemRoleAO data) {
        if ( data == null ) {
            return null;
        }
        SystemRoleBuilder systemRole = SystemRole.builder();
        systemRole.name( data.getName() );
        systemRole.code( data.getCode() );
        systemRole.status( data.getStatus() );
        return systemRole.build();
    }

父类代码如下,其中Model是一个开源的mybatisplus,数据库DO需要继承该Model可以使用基础的CRUD

@Getter
@Setter
public class AbstractDO<T extends Model<T>> extends Model<T> implements Serializable {

    @TableId(type =  IdType.AUTO)
    private Long id;
    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    private Date gmtCreate;
    /**
     * 修改时间
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date gmtModified; 
}

此时发现,父类字段并没有赋值,原因是因为lombok的build没有父类属性,直接产生BUG
lombok编译出的代码如下(为了阅读方便省去了set/set/tostr等)

public class SystemRole extends AbstractBaseDO<OpSystemRole> {
    private String name;
    private String code;
    private Integer status;
    private Integer deleted;

    public static SystemRole.SystemRoleBuilder builder() {
        return new SystemRole.SystemRoleBuilder();
    }

    public SystemRole() {
    }

    public SystemRole(String name, String code, Integer status, Integer deleted) {
        this.name = name;
        this.code = code;
        this.status = status;
        this.deleted = deleted;
    }

    public static class SystemRoleBuilder {
        private String name;
        private String code;
        private Integer status;
        private Integer deleted;

        SystemRoleBuilder() {
        }

        public SystemRole.SystemRoleBuilder name(String name) {
            this.name = name;
            return this;
        }

        public SystemRole.SystemRoleBuilder code(String code) {
            this.code = code;
            return this;
        }

        public SystemRole.SystemRoleBuilder status(Integer status) {
            this.status = status;
            return this;
        }

        public SystemRole.SystemRoleBuilder deleted(Integer deleted) {
            this.deleted = deleted;
            return this;
        }

        public SystemRole build() {
            return new SystemRole(this.name, this.code, this.status, this.deleted);
        }

        public String toString() {
            return "SystemRole.SystemRoleBuilder(name=" + this.name + ", code=" + this.code + ", status=" + this.status + ", deleted=" + this.deleted + ")";
        }
    }

三丶解决问题思路

知道问题的原因,方便解决问题

1.可以去掉pojo上的@Builder
2.让lombok能够在builder中使用父类属性
3.让mapstruct不使用Builder

第一种方式肯定是最直观的最简单的,但是我不想妥协
第二种方式,在lombok版本1.18.8中增加了@SuperBuilder,子类和父类全部加上@SuperBuilder,可以使子类的build包含父类的属性,但是这种方案不符合本项目,因为使用了mybatisplus,抽象DO必须继承第三方jar的Model,所以不行
第三种方案,网上查阅资料,外国老哥已经注意到这类问题,开发者提供了NoOpBuilderProvider的解决方案,官方文档也给出了介绍
但是我是没搞懂这怎么用,最后在mapstruct源码中发现了解决问题的方案
源码如下,可以看到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属性,开始我看的1.3.0.Final版本就没有,白白浪费时间,意思很明显,这就是是否开始构建者模式的开关,默认开始,直接关闭

@Retention(RetentionPolicy.CLASS)
@Target({})
@Experimental
public @interface Builder {

    /**
     * The name of the build method that needs to be invoked on the builder to create the type to be build
     *
     * @return the method that needs to tbe invoked on the builder
     */
    String buildMethod() default "build";

    /**
     * Toggling builders on / off. Builders are sometimes used solely for unit testing (fluent testdata)
     * MapStruct will need to use the regular getters /setters in that case.
     *
     * @return when true, no builder patterns will be applied
     */
    boolean disableBuilder() default false;
}

在mapper注解上使用

@Mapper(builder = @Builder(disableBuilder = true))
public interface SystemMapStruct {
    SystemRole convertSystemRole(SystemRoleAO data);
}

再看看编译后的代码,已经可以使用父类的属性

    @Override
    public OpSystemRole convertOpSystemRole(OpSystemRoleAO data) {
        if ( data == null ) {
            return null;
        }
        OpSystemRole opSystemRole = new OpSystemRole();
        opSystemRole.setId( data.getId() );
        opSystemRole.setName( data.getName() );
        opSystemRole.setCode( data.getCode() );
        opSystemRole.setStatus( data.getStatus() );
        return opSystemRole;
    }

总结

如果可以所有的父类都加@SuperBuilder,那可以这样解决,
如果不具备这个条件,可以使用builder = @Builder(disableBuilder = true) 来关闭mapstruct使用builder解决

上一篇下一篇

猜你喜欢

热点阅读