Mapstruct - 使用不同映射器中的方法映射对象的列表项
Mapstruct - Mapping an object's list item with a method in a different mapper
我有两个映射器 class,如下所示。
@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 方法吗?
如果不可能,我如何在不在 CountryMapper 中编写自定义映射方法的情况下将“countryLanguages.id.language.id”映射到“languages.languageId”?
按如下方式编辑 CountryMapper class 对我有用。但是我想知道是否有办法在不同的映射器中使用一个方法。
@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-other-mappers
我有两个映射器 class,如下所示。
@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 方法吗?
如果不可能,我如何在不在 CountryMapper 中编写自定义映射方法的情况下将“countryLanguages.id.language.id”映射到“languages.languageId”?
按如下方式编辑 CountryMapper class 对我有用。但是我想知道是否有办法在不同的映射器中使用一个方法。
@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-other-mappers