MapStruct的新手,我试图映射目标类中的对象列表,其中源包含单个值(包括其他String变量)。自定义实现也没有运气。任何建议实施的建议
public class Source {
String var1;
String var2;
String resourceCd1;
String resourceCd2;
String resourceCd3;
String resourceCd4;
String resourceCd5;
String resourceDesc1;
String resourceDesc2;
String resourceDesc3;
String resourceDesc4;
String resourceDesc5;
String var3;
String var4;
}
public class Target {
List<Resource> resources;
}
public class Resource {
String code;
String description;
}
期望是在目标中具有Resource对象的列表,包括代码和描述值,如下所示。
List<Resource> resources = new ArrayList<>();
Resource resource1 = new Resource();
resource1.code = soruce.resourceCd1;
resource1.description = soruce.resourceDesc1;
resources.add(resource1);
Resource resource2 = new Resource();
resource2.code = soruce.resourceCd2;
resource2.description = soruce.resourceDesc2;
resources.add(resource2);
Resource resource3 = new Resource();
resource3.code = soruce.resourceCd3;
resource3.description = soruce.resourceDesc3;
resources.add(resource3);
Resource resource4 = new Resource();
resource4.code = soruce.resourceCd4;
resource4.description = soruce.resourceDesc4;
resources.add(resource4);
Resource resource5 = new Resource();
resource5.code = soruce.resourceCd5;
resource5.description = soruce.resourceDesc5;
resources.add(resource5);
resourceCd1,..,resourceCd5和resourceDesc1,..,resourceDesc5来自源类
答案 0 :(得分:1)
开箱即用,无法使用MapStruct。 MapStruct支持bean-to-bean,可迭代到可迭代,可映射到地图。
当前正在构建中:bean-to-map ..准备就绪后,您可以通过流/收集器将地图转换为列表。