[英]Mapstruct - generate mappers for all classes inherited from a base class?
I have a Mapstruct mapper which I use to merge incoming requests with existing data - the mapper looks like this我有一个 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);
}
The BaseMapper
looks like this BaseMapper
看起来像这样
public interface BaseMapper<T extends BaseEntity> {
T merge(T var1, @MappingTarget T var2);
}
Considering that I have multiple entities like FooEntity
and all of them extend BaseEntity
, I have to define a mapper manually for each of these entities - something that I don't really need to do because the functionality doesn't change across classes.考虑到我有多个实体,比如
FooEntity
并且它们都扩展BaseEntity
,我必须为每个实体手动定义一个映射器——我真的不需要这样做,因为功能不会跨类改变。 Is there a way to define the Mapstruct properties on a global level (?) so that it automatically generates a mapper for every bean that extends from BaseEntity
?有没有办法在全局级别(?)定义 Mapstruct 属性,以便它为从
BaseEntity
扩展的每个 bean 自动生成一个映射器?
As you, I used BaseEntity on my older project.和你一样,我在旧项目中使用了 BaseEntity。 This is how I configured the mappers.
这就是我配置映射器的方式。
public interface BaseMapper {
BaseEntity toBaseEntity(BaseDTO baseDTO);
BaseDTO toBaseDTO(BaseEntity baseEntity);
}
on the foo Mapper you have to add the BaseMapper as config.在 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.