gpt4 book ai didi

java - 我可以使用 Mapstruct 在单个 Java 类中调用多个外部映射器吗?

转载 作者:行者123 更新时间:2023-12-02 01:42:56 27 4
gpt4 key购买 nike

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

例如。 PersonDTO 具有 "firstName""lastName""languageDTO""zipCodeDTO"

现在在我的 PersonMapper 中,我想使用我的 "LanguageMapper" 和我的 "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);
}

最佳答案

如果将多个映射器放入数组中,则可以在 uses 中添加它们:

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

关于java - 我可以使用 Mapstruct 在单个 Java 类中调用多个外部映射器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54197763/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com