2017-08-10 80 views
0

我試圖對象的映射爲對象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);  

回答

0

的問題是,你試圖映射SpeciesList<Logdeclaration>和MapStruct無法找到這樣的映射方法。爲了讓它工作,你可以在下面的方法添加到您的SpecyToLogDeclarationMapperApi

default List<Logdeclaration> SpeciesToLogDeclarationsToList(Species species, @Context ExtraFields extraFields) { 
    if (species == null) { 
     return null; 
    } 

    Logdeclaration logDeclaration = SpeciesToLogDeclarations(species, extraFields); 

    List<Logdeclaration> logdeclarations = new ArrayList<Logdeclaration>(); 
    logdeclarations.add(logDeclaration); 
    return logdeclarations; 
} 

這是一些額外的東西,我覺得你可以做些什麼來改善你的代碼,並使用MapStruct「更準確」:

  • 你將需要刪除的Mappers#getMapper(Class)使用時的組件模型不是default
  • 如果你不想在你的表達式中使用FQN,您可以使用Mapper#imports和MapStruct將在執行導入。
  • 當你有一個單一的源參數,你不必使用它的名字在映射

例如

@Mapping(target="fishcount", source = "species.qty") 

可以

@Mapping(target="fishcount", source = "qty")