gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:23:55 24 4
gpt4 key购买 nike

我有以下 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 接口(interface)也都存在。现在,我正在编写我的 mapstruct Mapper 接口(interface),该接口(interface)应用于将实体映射到 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 进行查找。您可以将其包装在另一个类中,并将其作为 @Context 注释为映射参数。 MapStruct 不会将此类视为源或目标。但是它会调用此类中的生命周期方法..看看这个示例:https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-jpa-child-parent/src .

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

关于java - 在mapstruct映射器中实现自定义行为的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57220625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com