簡體   English   中英

來自實體映射結構中的多個字符串的 Dto

[英]Dto from multiple string in Entity mapstruct

我一直在尋找這個問題,但找不到答案。

我想知道是否有一種方法可以將多個實體的字段映射到單個 DTO,但使用另一個映射器並映射到封裝的 DTO。

一個例子:

這是我的實體:

public class Identification{
    long dbId;
    String id;
    String type;
    String completeName;
    boolean status;
}

我的 DTO:

public class PersonEntity{
    String completeName;
    IdentificationEntity identificationEntity;
}

public class IdentificationEntity{
    String documentNumber;
    boolean status;
    String documentType;
}

我創建了我的映射器:

@Mapper(componentModel = "spring", uses = {IdentificationMapper.class})
public interface PersonMapper {



    PersonEntity toPersonEntity(Identification identification);
}
@Mapper(componentModel = "spring")
public interface IdentificationMapper {

    @Mapping(source = "id", target = "documentNumber")
    @Mapping(source = "type", target = "documentType")
    IdentificationEntity toIdentificationEntity(Identification identification);


}

但我不知道如何使用映射器從 PersonEntity 映射IdentificationEntity 我的意思是,如果有不使用@AfterMapping的方式,已經與標注用途嘗試和我真的不知道這是更多鈔票由qualifiedBy

@Mapper(componentModel = "spring")
public interface PersonMapper {


    @Mapping(target="identificationEntity", qualifiedBy=IdentificationMapper.class)
    PersonEntity toPersonEntity(Identification identification);
}

請幫忙解決這個問題。 :D

從我的理解,你想用的IdentificationMapper映射IdentificationIdentificationEntity

你幾乎明白了。

你需要告訴MapStruct映射identificationidentificationEntityPersonEntity

例如

@Mapper(componentModel = "spring", uses = {IdentificationMapper.class})
public interface PersonMapper {


    @Mapping(target = "identificationEntity", source = "identification")
    PersonEntity toPersonEntity(Identification identification);
}

IdentificationMapper保持不變。

關於qualifiedBy 當您想以特殊方式映射某些屬性時,這是必需的。 例如大寫某些字符串,但僅限於那些特定屬性。 在那里您需要使用 MapStruct @Qualifier來限定您的方法。 您可以在基於文檔中的限定符映射方法選擇中閱讀有關它們的更多信息

暫無
暫無

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

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