为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕妹3146593 2022-10-12 15:52:16
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。并且目标应该包含所有字段,即使这些字段在一个源中不可用
查看完整描述

1 回答

?
米脂

TA贡献1836条经验 获得超3个赞

尝试:


@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

   }



}


查看完整回答
反对 回复 2022-10-12
  • 1 回答
  • 0 关注
  • 628 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号