Mapstruct:如何映射子对象是列表并具有列表属性的层次结构
Mapstruct : How do I map hierarchical structure where the child object is a list and have attributes which are lists
我有一个场景需要从 DTO 对象映射到域对象。
我已经简化了下面的场景
class TopLevel {
private String attr1;
private String attr2;
private List<SecondLevel> secondLevel = new ArrayList<>();
class SecondLevel {
private String attr3;
private JsonNode additionalConfig;
private List<String> stringList = new ArrayList<>();
private List<ThirdLevel> thirdLevel = new ArrayList<>();
class ThirdLevel {
private String attr4;
private String attr5;
private List<String> anotherList = new ArrayList<>();
}
}
}
这需要映射到下面的域结构
class TopLevelDomain {
private ID guid1; //Generated Guid
private String attr1;
private String attr2;
private List<SecondLevelDomain> secondLevelDomain = new ArrayList<>();
}
class SecondLevelDomain {
private ID guid1; //From TopLevel Domain
private ID guid2; //Generated Guid
private String attr3;
private String stringListAsString; // List of Strings needs to be converted to string (JSON format)
private String additionalConfig; //JSON node needs to be converted to String (JSON format)
private List<ThirdLevelDomain> thirdLevelDomain = new ArrayList<>();
}
class ThirdLevelDomain {
private ID guid1; //From TopLevel Domain
private ID guid2; //From SecondLevel Guid
private ID guid3; //New Guid
private String attr4;
private String attr5;
private String anotherListAsString; // List of Strings needs to be converted to string (JSON format)
}
现在我需要做一个 qualifiedByName/Named 映射来获取第一关的 GUID。我也需要其他 2 个子级别的类似 GUID。
@Mapping(source = "topLevel", target = "guid1", qualifiedByName = "mapGuid1")
@Mapping(source = "topLevel", target = "secondLevelDomain", qualifiedByName = "secondLevelDomain")
TopLevelDomain toTopLevel(TopLevel topLevel)
如果我执行类似上述代码的操作,我看不出如何访问 @Mapping 注释中的二级属性 attr3、additionalConfig 和 List stringList
还有如何访问第三级属性 attr4、attr5 和 anotherList。我需要直接映射字符串矩阵,分配 guid 并为字符串列表和 JSONNodes
做一些自定义映射
这里有 2 件事你想做。
- 添加其他映射方法。 Mapstruct 将使用 user-defined 映射方法来生成新方法。
- 使用
@AfterMapping
传递生成的 GUID。
// custom mapping annotations
TopLevelDomain toTopLevel( TopLevel topLevel );
// custom mapping annotations
SecondLevelDomain toSecondLevel( TopLevel.SecondLevel secondLevel );
// custom mapping annotations
ThirdLevelDomain toThirdLevel( TopLevel.SecondLevel.ThirdLevel thirdLevel );
// custom mapping methods
@AfterMapping
default void handleGuidTransfer( TopLevelDomain topLevel ){
for ( SecondLevelDomain secondLevel: topLevel.getSecondLevelDomain() ) {
secondLevel.setGuid1( topLevel.getGuid1() );
for ( ThirdLevelDomain thirdLevel: secondLevel.getThirdLevelDomain() ) {
thirdLevel.setGuid1( topLevel.getGuid1() );
thirdLevel.setGuid2( secondLevel.getGuid2() );
}
}
}
我有一个场景需要从 DTO 对象映射到域对象。
我已经简化了下面的场景
class TopLevel {
private String attr1;
private String attr2;
private List<SecondLevel> secondLevel = new ArrayList<>();
class SecondLevel {
private String attr3;
private JsonNode additionalConfig;
private List<String> stringList = new ArrayList<>();
private List<ThirdLevel> thirdLevel = new ArrayList<>();
class ThirdLevel {
private String attr4;
private String attr5;
private List<String> anotherList = new ArrayList<>();
}
}
}
这需要映射到下面的域结构
class TopLevelDomain {
private ID guid1; //Generated Guid
private String attr1;
private String attr2;
private List<SecondLevelDomain> secondLevelDomain = new ArrayList<>();
}
class SecondLevelDomain {
private ID guid1; //From TopLevel Domain
private ID guid2; //Generated Guid
private String attr3;
private String stringListAsString; // List of Strings needs to be converted to string (JSON format)
private String additionalConfig; //JSON node needs to be converted to String (JSON format)
private List<ThirdLevelDomain> thirdLevelDomain = new ArrayList<>();
}
class ThirdLevelDomain {
private ID guid1; //From TopLevel Domain
private ID guid2; //From SecondLevel Guid
private ID guid3; //New Guid
private String attr4;
private String attr5;
private String anotherListAsString; // List of Strings needs to be converted to string (JSON format)
}
现在我需要做一个 qualifiedByName/Named 映射来获取第一关的 GUID。我也需要其他 2 个子级别的类似 GUID。
@Mapping(source = "topLevel", target = "guid1", qualifiedByName = "mapGuid1")
@Mapping(source = "topLevel", target = "secondLevelDomain", qualifiedByName = "secondLevelDomain")
TopLevelDomain toTopLevel(TopLevel topLevel)
如果我执行类似上述代码的操作,我看不出如何访问 @Mapping 注释中的二级属性 attr3、additionalConfig 和 List stringList
还有如何访问第三级属性 attr4、attr5 和 anotherList。我需要直接映射字符串矩阵,分配 guid 并为字符串列表和 JSONNodes
做一些自定义映射这里有 2 件事你想做。
- 添加其他映射方法。 Mapstruct 将使用 user-defined 映射方法来生成新方法。
- 使用
@AfterMapping
传递生成的 GUID。
// custom mapping annotations
TopLevelDomain toTopLevel( TopLevel topLevel );
// custom mapping annotations
SecondLevelDomain toSecondLevel( TopLevel.SecondLevel secondLevel );
// custom mapping annotations
ThirdLevelDomain toThirdLevel( TopLevel.SecondLevel.ThirdLevel thirdLevel );
// custom mapping methods
@AfterMapping
default void handleGuidTransfer( TopLevelDomain topLevel ){
for ( SecondLevelDomain secondLevel: topLevel.getSecondLevelDomain() ) {
secondLevel.setGuid1( topLevel.getGuid1() );
for ( ThirdLevelDomain thirdLevel: secondLevel.getThirdLevelDomain() ) {
thirdLevel.setGuid1( topLevel.getGuid1() );
thirdLevel.setGuid2( secondLevel.getGuid2() );
}
}
}