需要使用mapstruct将两个源对象合并到目标对象中,这里source1中的一个字段具有List,而source2中的字符串是字符串

问题描述 投票:0回答:1
public class Source1 {
    private String name;               //srihari  
    private List<String> city_names;   //city_names.add("hyderabad-india")
 }
public class Soruce2 {
    private String name;
    private String city_name;            //hyderabad-india
    private List<String> technologies;   //Arrays.asList("java","mapstruct")
 }
public class Target {
    private String name;            // Result: srihari 
    private String city_names;      // Result: hyderabad-india
    private String technologies;    // Result: java, mapstruct
}`

list只有一个值表示list.size()= 1。如果source1名称为空,则必须从source2获取。并且目标应该包含所有字段 - 尽管这些字段在一个源中不可用

java mapstruct
1个回答
2
投票

尝试:

@Mapper
public interface MyMapper{

   // will map all other fields that you specify
   @Mapping( target = "city_names", ignore = true )
   @Mapping( target = "technologies", ignore = true )
   Target map(Source1 s1, Soruce2 s2);

   default map(Source1 s1, Soruce2 s2, @MappingTarget Target t) {
      // do whatever you like with city_names and technologies
   }


}
© www.soinside.com 2019 - 2024. All rights reserved.