Mapstruct-映射器对象需要对象字段的值映射

时间:2020-05-09 07:06:26

标签: mapstruct

我有一个对象SetEvent EVENT_PALLET_AFTER_GETTING_POKEBALLS,我试图将其映射到.next。现在,这个A有一个名为B的枚举,带有一些值。我想使用A将它们映射到field1中的其他枚举值。到目前为止,以下是我的代码:

@ValueMappings

当我尝试编译它时,出现错误:

B

1 个答案:

答案 0 :(得分:3)

解决此问题的一种方法是执行以下操作:

@Mapper(componentModel = "spring",  uses = {SomeUtil.class}, unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface Mapper {
    @Mapping(source = "field1", target = "field1", nullValuePropertyMappingStrategy = IGNORE)
    @Mapping(source = "field2", target = "field2", nullValuePropertyMappingStrategy = IGNORE)
    B map(A a);
}

在SomeUtil.class中:

@Mapper(componentModel = "spring")
public interface SomeUtil {

    @ValueMappings({
        @ValueMapping(source = "field1.some1", target = "diff1"),
        @ValueMapping(source = "field1.some2", target = "diff1"),
        @ValueMapping(source = "field1.some3", target = "diff1"),
        @ValueMapping(source = "field1.some4", target = "diff2"),
    })
    b.field1 map(a.field1 field);
}