【问题标题】:Rewriting collection mapping from orika to mapstruct重写从 orika 到 mapstruct 的集合映射
【发布时间】:2021-12-18 13:01:46
【问题描述】:

你能告诉我如何使用 @Mapping 重写从 orika 到 mapstruct 的映射。

factory.classMap(SomeEntity.class, SomeDto.class)
         .fieldAToB("items{innerItem.id}", "innerItemIds{}")
         .byDefault().register();

不要使用其他方法。

这是否存在写类似的方式

@Mapping(source = "items{innerItem.id}", target = "innerItemIds{}")
SomeDto map(SomeEntity entity);

【问题讨论】:

    标签: java mapstruct orika


    【解决方案1】:

    我并不完全熟悉 Orika 的工作原理。但是,在 MapStruct 中,您可以为 MapStruct 无法执行的映射提供自定义方法。没有其他方法,没有其他方法。

    在您的情况下,您需要执行以下操作:

    @Mapper
    public interface MyMapper {
    
    
        @Mapping(target = "innerItemIds", source = "items")
        SomeDto map(SomeEntity entity);
    
        default String map(InnterItem item) {
            return item == null ? null : item.getId();
        }
    
    }
    

    您将使用@Mapping 告诉 MapStruct 它需要将items 集合映射到innerItemIds。我假设itemsCollection<InnerItem>innerItemIdsCollection<String>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-30
      • 2015-02-07
      • 1970-01-01
      • 2021-05-18
      • 2018-10-05
      • 2017-03-22
      • 2017-07-02
      • 2018-08-24
      相关资源
      最近更新 更多