【发布时间】:2019-07-25 14:43:42
【问题描述】:
使用MapStruct,并希望将常量/参数传递给自定义映射器
我的目的地有一个Map<String, Restriction> restrictions 类型的字段。源可能有 2-3 个字符串字段需要映射到映射中的单个限制。 Map 的键只是一个常数。使用 Map-struct Mapping 注释,我想将此键传递给自定义映射器,以便我可以在 Map 中创建初始值或检索 Map 中的现有值。
@Mappings({
@Mapping(source="source.someField", target="restrictions", constant="someKey"),
@Mapping(source="source.startDate", target="restrictions", constant="someKey"),
@Mapping(source="source.EndDate", target="restrictions", constant="someKey")
})
public abstract void restrictToClassA(SomeDataEntity source, @MappingTarget ClassA destination);
【问题讨论】: