我有兩個映射器類,如下所示。
@Mapper
public interface CountryLanguageMapper {
CountryLanguageMapper INSTANCE = Mappers.getMapper(CountryLanguageMapper.class);
List<CountryLanguageResponse> toResponseList(List<CountryLanguage> source);
@Mapping(target = "languageId", source = "id.language.id")
CountryLanguageResponse toResponse(CountryLanguage source);
}
@Mapper
public interface CountryMapper {
CountryMapper INSTANCE = Mappers.getMapper(CountryMapper.class);
List<CountryListResponse> toResponseList(List<Country> source);
@Mapping(target = "languages", source = "countryLanguages") // how should it be?
CountryResponse toResponse(Country source);
CountryListResponse toListResponse(Country source);
@Mapping(target = "languages", source = "countryLanguages")
CountryCreateResponse toCreateResponse(Country source);
Country fromCreateRequest(CountryCreateRequest source);
void updateFromUpdateRequest(CountryUpdateRequest source, @MappingTarget Country target);
}
我可以讓 CountryMapper#toResponse 方法在映射 countryLanguages 欄位時使用 CountryLanguageMapper#toResponseList 方法嗎?
如果不可能,我如何將“countryLanguages.id.language.id”映射到“languages.languageId”而不在 CountryMapper 中撰寫自定義映射方法?
如下編輯 CountryMapper 類對我有用。但我想知道是否有辦法在不同的映射器中使用方法。
@Mapper
public interface CountryMapper {
CountryMapper INSTANCE = Mappers.getMapper(CountryMapper.class);
List<CountryListResponse> toResponseList(List<Country> source);
@Mapping(target = "languages", source = "countryLanguages", qualifiedByName = "toCountryLanguageResponseList")
CountryResponse toResponse(Country source);
CountryListResponse toListResponse(Country source);
@Mapping(target = "languages", source = "countryLanguages")
CountryCreateResponse toCreateResponse(Country source);
@Mapping(target = "id", ignore = true)
Country fromCreateRequest(CountryCreateRequest source);
void updateFromUpdateRequest(CountryUpdateRequest source, @MappingTarget Country target);
@IterableMapping(qualifiedByName = "toCountryLanguageResponse")
@Named("toCountryLanguageResponseList")
List<CountryLanguageResponse> toCountryLanguageResponseList(Collection<CountryLanguage> source);
@Mapping(target = "languageId", source = "id.language.id")
@Named("toCountryLanguageResponse")
CountryLanguageResponse toCountryLanguageResponse(CountryLanguage source);
}
提前致謝 :)
uj5u.com熱心網友回復:
這可以通過uses指令實作。
例如:
@Mapper(uses=CountryLanguageMapper.class)
public interface CountryMapper {
有關更多資訊,請參見此處: https ://mapstruct.org/documentation/dev/reference/html/#invoking-other-mappers
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/439922.html
