我試圖對象的映射爲對象B.列表 我有一個從對象的映射爲對象B.使用mapstruct如何將對象映射爲對象的彈簧組件映射器列表
一個映射器我嘗試了很多不同的方法,例如 嘗試創建一個使用'表達式'的對象A的列表 和'qualifiedByName'但這不起作用,因爲我認爲 當您使用表達式/ qualifiedByName時,您無法使用 自定義映射器(我可能在這裏是錯的?)
我也試圖從t調用映射器他使用@aftermapper方法使用 'mappers.getMapper'來獲取目標映射器上的句柄 但我發現映射器中使用的spring bean 沒有被填充的地方。在映射映射使 意義,我可以調用目標映射器與源 然後將目標添加到列表中。所以我希望 是另一種從我的映射器獲取mapper組件句柄的方法。
我所有的映射工具使用 @Mapper(componentModel="spring",
所有建議,歡迎 下面是一個顯示問題的代碼示例。
問候, 德克蘭
// SPECIESTOLOGSPECY.JAVA
// From this mapper I want to call SpecyToLogDeclarationMapperApi mapper
// to map ‘species’ to ‘logdeclarations’ which is a list of logdeclaration
// you can see want I am trying to do in the aftermapping method
// where I map species to logdeclaration and then put this into a list
// unfortunately I need other mapping components (ConfigMapperFromCode & SpecyToFishDeclarationMapperApi)
// to be autowired into SpecyToLogDeclarationMapperApi and this is not happening.
// is there another way to get a handle to the spring managed
// SpecyToLogDeclarationMapperApi mapper ?
@Mapper(componentModel="spring",
uses = {
ConfigMapperFromCode.class,
GeoInfoMapper.class,
SpecyToLogDeclarationMapperApi.class
})
public interface SpeciesToLogSpecy {
SpecyToLogDeclarationMapperApi MAPPER = Mappers.getMapper(SpecyToLogDeclarationMapperApi.class);
@Mappings(
{
@Mapping(target="createdate", expression = "java(java.sql.Timestamp.valueOf(java.time.LocalDateTime.now()))"),
@Mapping(target="speciesid", qualifiedByName={"ConfigMapperFromCode", "speciesIdFromCodeAsDecimal"}, source = "species.speciesCode"),
@Mapping(target="unitweight", constant = "1"),
@Mapping(target = "inactiveind", constant = "N"),
@Mapping(target = "unitdefaultind", constant = "Y"),
@Mapping(target = "sectiontypeid", expression = "java(new BigDecimal(ie.gov.agriculture.fisheries.logsheet.mapping.constants.MappingConstants.LOG_SPECIES_SECTION_TYPE_SHEETDECLARATION))"),
@Mapping(target = "unituomid", expression = "java(new BigDecimal(ie.gov.agriculture.fisheries.logsheet.mapping.constants.MappingConstants.LOGSHEET_CATCHUNITS_KG))"),
@Mapping(target="catchtypeid", qualifiedByName={"ConfigMapperFromCode", "returnCatchTypeId"}, source = "species.spType"),
@Mapping(target="legalfishsizetypeid", qualifiedByName={"ConfigMapperFromCode", "legalFishSizeTypeIdFromCode"}, source = "species.fishSizeClass"),
@Mapping(target="presenttypeid", qualifiedByName={"ConfigMapperFromCode", "presentationTypeIdFromCode"}, source = "species.presentation.presentationType"),
//@Mapping(target="logdeclarations", source = "species")
}
)
Logspecy speciesToLogspecy(Species species, @Context ExtraFields extraFields);
@AfterMapping
default void afterMap(@MappingTarget Logspecy logspecy, @Context ExtraFields extraFields){
Logdeclaration logDeclaration = MAPPER.SpeciesToLogDeclarations(species, extraFields);
List<Logdeclaration> logdeclarations = new ArrayList<Logdeclaration>();
logdeclarations.add(logDeclaration);
logSpecy.setLogdeclarations(logdeclarations);
{
// SPECYTOLOGDECLARATIONMAPPERAPI.JAVA
@Mapper(componentModel="spring",
uses = {
ConfigMapperFromCode.class,
SpecyToFishDeclarationMapperApi.class
}
)
public interface SpecyToLogDeclarationMapperApi {
@Mappings(
{
@Mapping(target="createdate", expression = "java(java.sql.Timestamp.valueOf(java.time.LocalDateTime.now()))"),
@Mapping(target="geartypeid", qualifiedByName={"ConfigMapperFromCode", "gearIdFromCode"}, source = "species.gearType"),
@Mapping(target="fishcount", source = "species.qty"),
@Mapping(target = "inactiveind", constant = "N"),
@Mapping(target="packagetypeid", qualifiedByName={"ConfigMapperFromCode", "packagingTypeIdFromCode"}, source = "species.presentation.packaging"),
@Mapping(target="packagecount", source = "species.presentation.pkgunit"),
@Mapping(target="avgpackageweight", source = "species.presentation.pkgUnitWeight"),
@Mapping(target="conversionfactor", source = "species.presentation.convFactor"),
@Mapping(target="fishdeclaration", source = "species.geoInfo")
}
)
Logdeclaration SpeciesToLogDeclarations (Species species, @Context ExtraFields extraFields);