在 mapstruct 映射器中实现自定义行为的问题

Problems in Implementing a Custom Behaviour in mapstruct Mapper

我有以下 DTO classes:

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

还有它们对应的实体classes:

@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 的适当实现的工厂 class。

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

将 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 clean compile

源将在 targeted/generated-sources/annotations

中生成

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

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

我最终实现了以下实现:

@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);
}

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