映射嵌套豆列表:“ org.mapstruct.Mapping”

时间:2019-12-09 13:14:00

标签: java spring spring-boot mapping mapstruct

我想使用“ org.mapstruct.Mapping”将Abc类映射到AbcDTO

class Abc {
    private List<Xyz> xyz = null;
    private String uvw;
    private String cde;
}

class AbcDTO{
    private List<XyzDTO> xyz = null;
    private String uvw;
    private String cde;
}

class Xyz{
    private String type;
    private String value;
    private String docId;
}

class  XyzDTO{
    private String type;
    private String value;
    private DocDTO document;
}

我试图通过使用注释来映射类:

    @Mappings({
            @Mapping(source = "xyz.docId", target = "xyz.doc")
    })
 abcDTO abcToabcDTO(abc abc)

有人可以帮助我如何遍历嵌套的bean并将docId映射到doc? 如果名称相同,它们会自动映射,但我想从docId映射到doc

2 个答案:

答案 0 :(得分:1)

当您想要地图列表时,可以定义它:

@Mapping(source="docId", target="doc")
XyzDTO xyzToXyzDTO(XyZ xyz);

@Mapping(source="xyz", target="xyz") //useless if two lists got same name, but good for comprehention
AbcDTO abcToAbcDTA(Abc abc);

答案 1 :(得分:0)

应如下所示( 您可以尝试一下):

@Mappings({
    @Mapping(target="doc", source="abc.docId")
  })
AbcDTO abcToabcDTO(Abc abc);```