【问题标题】:How can I map from a collection to a collection property?如何从集合映射到集合属性?
【发布时间】:2021-12-11 14:18:22
【问题描述】:

假设我有以下映射目标。

class Some {

    private List<Other> others;
}

如何从Others 的Iterable 映射到others 属性?

我可以这样做吗?

@Mapper
interface SomeMapper {

    // map source to target.others
    Some mapOthers(Iterable<? extends Other> source, @MappingTarget Some target);
}

【问题讨论】:

    标签: mapstruct


    【解决方案1】:

    当您想将源参数映射到目标属性时,如果源参数与目标属性的名称不匹配,则应定义@Mapping。在这种情况下,您可以执行以下操作:

    @Mapper
    interface SomeMapper {
    
        @Mapping(target = "others", source = "source")
        Some mapOthers(Iterable<? extends Other> source, @MappingTarget Some target);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 2019-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多