【问题标题】:Problems in Implementing a Custom Behaviour in mapstruct Mapper在 mapstruct Mapper 中实现自定义行为的问题
【发布时间】:2019-12-04 19:59:55
【问题描述】:

我有以下 DTO 类:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Conclusion {

    private Integer id;
    // ......
    private List<CType> cTypes;
}

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class CType {

    private Integer id;
    // ......
    private VType vType;
}

还有它们对应的实体类:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "conclusion")
public class Conclusion {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, updatable = false)
    private Integer id;

    // ......

    @OneToMany
    @JoinColumn(name = "id")
    private List<CTypeEntity> cTypeEntities;
}

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "c_types")
public class CTypeEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, updatable = false)
    private Integer id;

    // ......

    @Column(name = "v_type_id")
    private Integer vTypeId;
}

此外,所有相应的 Dao 和 JPA Repository 接口都存在。 现在,我正在编写我的 mapstruct Mapper 接口,它应该用于将实体映射到 DTO,反之亦然。这是映射器:

@Mapper
public interface ConclusionMapper {

    @Mappings({
            @Mapping(target = "cTypes", source = "cTypeEntities")
    })
    Conclusion toConclusion(ConclusionEntity entity);

    List<Conclusion> toConclusionList(List<ConclusionEntity> entities);

    @Mappings({
            @Mapping(target = "cTypeEntities", source = "cTypes")
    })
    ConclusionEntity fromConclusion(Conclusion conclusion);

    List<ConclusionEntity> fromConclusionList(List<Conclusion> conclusions);

    @Mappings({
            @Mapping(target = "cType", source = "vTypeId", qualifiedByName = "formVType")
    })
    ConclusionTaxType toCType(CTypeEntity cTypeEntity);

    List<CType> toCTypes(List<CTypeEntity> cTypeEntities);

    CTypeEntity fromCType(CType cType);

    List<CTypeEntity> fromCTypeList(List<CType> cTypes);

    @Named("formVType")
    default VType formVType(CTypeEntity entity) {
        // TODO: instantiate a DAO which will return an instance of VType
        VTypeDao dao; // instantiate somehow

        return vTypeDao.findById(entity.getVId()); 
    }
}

VTypeDao 看起来像这样:

public interface VTypeDao {
    VType findById(int id);

    boolean exists(int id);

    List<VType> findAll();
}

@Component
public class VTypeDaoImpl implements VTypeDao {
    private final VTypeRepo vTypeRepo;

    public VTypeDaoImpl(VTypeRepo vTypeRepo) {
        this.vTypeRepo = vTypeRepo;
    }

    // ............................. method implementations
}

我的问题是:如何实例化 VTypeDao 的对象(或者至少是 VTypeRepo,以便我可以将 if 作为参数传递给 VTypeDaoImpl)?

没有工厂类用于获取 VTypeDao 的适当实现。

编辑: VTypeDao 及其实现是我项目的第三方组件。

【问题讨论】:

  • VTypeDao 和 MapStruct 有什么关系?顺便说一句:您可以向名为 componentmodel 的 @Mapper 注释添加一个参数。您可以将其设置为 spring,这使映射器成为 spring 组件。
  • VTypeDao 和 MapStruct 之间没有关系。 VTypeDao 是一种第三方 DAO,我需要在我的 MapStruct 中使用它。据我了解,用 componentmodel 注释我的 MapStruct 只允许我在某些 DAO 实现中使用它。我的目标是相反的:我希望在我的 MapStruct 中使用 VTypeDao。

标签: java spring spring-boot spring-data-jpa mapstruct


【解决方案1】:

从您的评论中,我了解到您想在映射过程中查找您的 VTypeDao。您可以将其包装在另一个类中并将其作为 @Context 注释为映射参数。 MapStruct 不会将此类视为源或目标。但是它会在这个类中调用生命周期方法。看看这个例子:https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-jpa-child-parent/src

它基于 jpa,但您可以轻松地将其映射到您的问题..

【讨论】:

    【解决方案2】:

    我最终得到了以下实现:

    @Mapper
    public interface ConclusionMapper {
    
        @Mappings({
               @Mapping(target = "cTypes", source = "cTypeEntities")
        })
        Conclusion toConclusion(ConclusionEntity entity);
    
        List<Conclusion> toConclusionList(List<ConclusionEntity> entities);
    
        @InheritInverseConfiguration
        ConclusionEntity fromConclusion(Conclusion conclusion);
    
        List<ConclusionEntity> fromConclusionList(List<Conclusion> conclusions);
    
        @Mappings({
                @Mapping(target = "vType", ignore = true)
        })
        ConclusionTaxType toCType(CTypeEntity cTypeEntity);
    
        List<CType> toCTypes(List<CTypeEntity> cTypeEntities);
    
        @Mappings({
                @Mapping(target = "vTypeId", source = "vType.id")
        })
        CTypeEntity fromCType(CType cType);
    
        List<CTypeEntity> fromCTypeList(List<CType> cTypes);
    }
    

    所以我只是忽略了实体到 DTO 映射中的 vType 成员,并将其手动放入我的 DAO 中,因为这是最简单的做法。

    【讨论】:

      【解决方案3】:

      将 map-struct 与 spring 集成:
      http://mapstruct.org/documentation/stable/reference/html/#using-dependency-injection

      @Mapper 替换为下面的

      @Mapper(componentModel = "spring")    
      

      并在VTypeDaoImpl下面添加

      @Autowired  
      private ConclusionMapper conclusionMapper;
      

      执行下面的maven命令(假设你使用的是maven)

      mvn 清理编译

      源将在targeted/generated-sources/annotations中生成

      【讨论】:

      • 好像有误会。我不想在 VTypeDao 中使用我的 MapStruct,我想要相反:在我的 MapStruct 中使用 VTypeDAo。通过将组件模型添加到我的 mapstruct 并在 VTypeDaoImpl 中自动装配它对我没有帮助。顺便说一句,VTypeDao 及其实现是我项目的第三方组件。
      猜你喜欢
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-06
      • 2016-07-31
      • 1970-01-01
      • 2021-12-05
      相关资源
      最近更新 更多