我有一个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映射器上,您必须将BaseMapper添加为config。
@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)
}