我有两个映射器类,如下所示。
@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);
}
映射CountryLanguageMapper#toResponseList字段时,我可以让CountryMapper#toResponse方法使用CountryLanguageMpper#toResponseList方法吗?
如果不可能的话;countryLanguages.id.language.id"至";languages.languageId";而不在CountryMapper中编写自定义映射方法?
按如下方式编辑CountryMapper类对我来说很有效。但我想知道是否有办法在不同的mapper中使用方法。
@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);
}
提前感谢:(
这在uses
指令中是可能的。
例如:
@Mapper(uses=CountryLanguageMapper.class)
public interface CountryMapper {
有关更多信息,请参阅此处:https://mapstruct.org/documentation/dev/reference/html/#invoking-其他映射器