需要使用 mapstruct 将两个源对象合并为目标对象,这里 source1 中的一个字段有

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.size()=1。如果 source1 名称为空,则必须取自 source2。并且目标应该包含所有字段,即使这些字段在一个源中不可用


慕妹3146593
浏览 629回答 1
1回答

米脂

尝试:@Mapperpublic interface MyMapper{&nbsp; &nbsp;// will map all other fields that you specify&nbsp; &nbsp;@Mapping( target = "city_names", ignore = true )&nbsp; &nbsp;@Mapping( target = "technologies", ignore = true )&nbsp; &nbsp;Target map(Source1 s1, Soruce2 s2);&nbsp; &nbsp;default map(Source1 s1, Soruce2 s2, @MappingTarget Target t) {&nbsp; &nbsp; &nbsp; // do whatever you like with city_names and technologies&nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java