簡體   English   中英

映射器沒有使用另一個映射器,如何使用另一個映射器的映射器?

[英]Mapper is not using another mapper, how to use a mapper from another mapper?

我正在試驗 MapStruct,我想讓一個映射器使用另一個映射器將多個對象組合成一個。 對於我的測試,我有三個域對象DomainObject1DomainObject2DomainObject3 我想將DomainObject1轉換為 DTO TransferObjectA ,它有一個包含第二個 DTO TransferObjectB的字段,它是使用DomainObject2DomainObject3構造的。

我有兩個映射器,一個將DomainObject2DomainObject3轉換為TransferObjectB ,它會忽略DomainObject2中的一個字段,因此它不會被轉換:

@Mapper
public interface ObjectBMapper {

  @Mapping(target = "field1", ignore = true)
  TransferObjectB domainObject2ToTransferObjectB(DomainObject2 domainObject2, DomainObject3 domainObject3);
}

第二個映射器將DomainObject1轉換為TransferObjectA ,它也接受DomainObject2DomainObject3 ,因此它們可以使用上述映射器進行轉換並放在生成的TransferObjectA上:

@Mapper(uses = ObjectBMapper.class)
public interface ObjectAMapper {

  TransferObjectA domainObject1ToTransferObjectA(DomainObject1 domainObject1, DomainObject2 domainObject2, DomainObject3 domainObject3);
}

但是, ObjectBMapper似乎沒有被ObjectAMapper使用,並且field1被轉換(這導致錯誤,因為它是一個enum )。 為什么不使用ObjectBMapper以及如何確保ObjectAMapper將使用它?

MapStruct 當前只能使用具有單個源參數的其他映射器。 有一個關於此功能的開放功能請求mapstruct/mapstruct#2081

您可以做的是使用包裝器 object 而不是多個源參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM