簡體   English   中英

Mapstruct - 為從基礎 class 繼承的所有類生成映射器?

[英]Mapstruct - generate mappers for all classes inherited from a base class?

我有一個 Mapstruct 映射器,用於將傳入請求與現有數據合並 - 映射器看起來像這樣

@Mapper(uses = ProtoMapperUtil.class,
    collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
    unmappedTargetPolicy = ReportingPolicy.IGNORE,
    nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
public interface FooEntityMapper extends BaseMapper<FooEntity> {
    FooEntityMapper INSTANCE = Mappers.getMapper(FooEntityMapper.class);
}

BaseMapper看起來像這樣

public interface BaseMapper<T extends BaseEntity> {
    T merge(T var1, @MappingTarget T var2);
}

考慮到我有多個實體,比如FooEntity並且它們都擴展BaseEntity ,我必須為每個實體手動定義一個映射器——我真的不需要這樣做,因為功能不會跨類改變。 有沒有辦法在全局級別(?)定義 Mapstruct 屬性,以便它為從BaseEntity擴展的每個 bean 自動生成一個映射器?

和你一樣,我在舊項目中使用了 BaseEntity。 這就是我配置映射器的方式。

public interface BaseMapper {

    BaseEntity toBaseEntity(BaseDTO baseDTO);
  
    BaseDTO toBaseDTO(BaseEntity baseEntity);

}    

在 foo Mapper 上,您必須將 BaseMapper 添加為配置。

@Mapper(
    config = BaseMapper.class,
    uses = { ProtoMapperUtil.class,
             collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
             unmappedTargetPolicy = ReportingPolicy.IGNORE,
             nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS})
public interface FooEntityMapper {

    FooEntityMapper INSTANCE = Mappers.getMapper(FooEntityMapper.class);

    //I don't remember if you need or not to declare the @InheritConfiguration
    //@InheritConfiguration(name = "toBaseDTO")
    FooDTO toFooDTO(FooEntity fooEntity)

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM