使用maps构造如何使用spring组件映射器将对象映射到对象列表

时间:2017-08-10 13:51:09

标签: mapstruct

我正在尝试将对象A映射到对象B的列表。 我有一个映射器,它从对象A映射到对象B.

我尝试了许多不同的方法 尝试创建一个包含一个对象的列表A使用'表达式' 并且' qualifiedByName'但这不起作用,因为我认为 当您使用表达式/ qualifiedByName时,您无法使用 自定义映射器(我可能在这里错了?)

我还尝试使用@aftermapper方法调用mapper 'mappers.getMapper'获取目标映射器的句柄 但是我发现了映射器中使用的spring bean 哪里没有人口。后映射中的映射使 感觉我可以用源调用目标映射器 然后将目标添加到列表中。所以我希望那里 是另一种从映射器获取映射器组件的方法。

我所有的地图制作者都在使用    @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);    

0 个答案:

没有答案