首页 > 解决方案 > 我可以使用 Mapstruct 在单个 Java 类中调用多个外部映射器吗?

问题描述

我目前正在映射具有两个“子 DTOS”的 DTO。

例如。 PersonDTO"firstName",,,, "lastName"_ "languageDTO"_"zipCodeDTO"

现在在我的 PersonMapper 中,我想使用 my"LanguageMapper"和 my "ZipCodeMapper".

@Mapper(uses = "")我只能调用一个外部映射器在我的课堂上使用。

预先感谢您的帮助

这是我的 DTO 的代码

这是我的 ZipCodeDTO:

class ZipCodeDTO {
    public static final String FIELD_SEPARATOR = "__";
    private String favouriteZipCode;
    private String cityName;
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFavouriteZipCode() {
        return favouriteZipCode;
    }

    public void setFavouriteZipCode(String favouriteZipCode) {
        this.favouriteZipCode = favouriteZipCode;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    @Override
    public String toString() {
        return id + FIELD_SEPARATOR + favouriteZipCode + FIELD_SEPARATOR + cityName;

    }

}

这是我的 PersonDTO:

class PersonDTO
{

    public static final String FIELD_SEPARATOR = "__";
    private int id;
    private String firstName;
    private String lastName;
    private PictureDTO pictureDTO = new PictureDTO();
    private ZipCodeDTO zipCodeDTO = new ZipCodeDTO();
    private List<PersonLanguageDTO> personLanguageDTOList = new ArrayList<>();

    public List<PersonLanguageDTO> getPersonLanguageDTOList() {
        return personLanguageDTOList;
    }

    public void setPersonLanguageDTOList(List<PersonLanguageDTO> personLanguageDTOList) {
        this.personLanguageDTOList = personLanguageDTOList;
    }

    public PictureDTO getPictureDTO() {
        return pictureDTO;
    }

    public void setPictureDTO(PictureDTO pictureDTO) {
        this.pictureDTO = pictureDTO;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public ZipCodeDTO getZipCodeDTO() {
        return zipCodeDTO;
    }

    public void setZipCodeDTO(ZipCodeDTO zipCodeDTO) {
        this.zipCodeDTO = zipCodeDTO;
    }
}

这是我的 PersonLanguageDTO

class PersonLanguageDTO{

    public static final String FIELD_SEPARATOR = "__";
    private String knowledge;
    private int personId;
    private LanguageDTO languageDTO = new LanguageDTO();

    public String getKnowledge() {
        return knowledge;
    }

    public void setKnowledge(String knowledge) {
        this.knowledge = knowledge;
    }

    public int getPersonId() {
        return personId;
    }

    public void setPersonId(int personId) {
        this.personId = personId;
    }

    public LanguageDTO getLanguageDTO() {
        return languageDTO;
    }

    public void setLanguageDTO(LanguageDTO languageDTO) {
        this.languageDTO = languageDTO;
    }

    @Override
    public String toString() {
        return knowledge + FIELD_SEPARATOR + personId + FIELD_SEPARATOR + languageDTO;

    }
}

这个,我试过了

@Mapper (componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.ERROR, uses = ZipCodeMapper.class +PersonLanguageMapper.class)
public interface PersonMapper {

@Mappings({

    @Mapping(source = "zipCode" , target = "zipCodeDTO" ),
    @Mapping(source = "", target = "pictureDTO")

})

PersonDTO toDTO(Person person);
}

标签: javaintellij-ideapluginsmapstruct

解决方案


uses如果将它们放在一个数组中,则可以添加多个 Mapper :

@Mapper (componentModel = "spring", 
         unmappedTargetPolicy = ReportingPolicy.ERROR,
         uses = { ZipCodeMapper.class, PersonLanguageMapper.class })
public interface PersonMapper {
    // Your code here
}

推荐阅读