2016-08-22 750 views
1

我是新來的MapStruct API,任何人都可以說如何做嵌套映射。 我有兩個類,一個是我的實際purchaseOrder類,它是我的目標類,另一個是EDPurchaseOrder類,它被稱爲源文件,這裏不用擔心我使用的命名約定,只需使用源文件和目標文件即可。Mapstruct中的嵌套映射

源類
源類EDCustomerOrder及其引用類

public class EDCustomerOrder{ 
     private Integer orderID; 
     private String orderNumber; 
     private BigDecimal orderTotalQty; 
     private String UOM; 
     private PickupDTO pickupPoints; 
     private Integer supplierID; 
     private String supplierName; 
     private String supplierNature; 
     private EDAddress supplierEDAddress; 
    } 

    public class EDPickup{ 
     private List<EDPOItem> items; 
    } 

    public class EDAddress{ 
     private String addressLine1; 
     private String addressLine2; 
     private String addressLine3; 
     private String city; 
     private String state; 
     private string countryCode; 
     private String country; 
     private String postalCode; 
    } 

    public class EDPOItem{ 
     private Integer itemID; 
     private String itemCode; 
     private String itemDescription; 
     private Integer itemQuantity; 
    } 

目標類
這裏我的目標類CustomerOrder和它引用類

public class CustomerOrder{ 
     private Integer orderID; 
     private String orderNumber; 
     private List<Pickup> pickupPoints; 
     private Supplier supplierDetail; 
    } 

    public class Pickup{ 
     private Integer pickupID; 
     private Integer pickupLocationNumber; 
     private List<POItem> items; 
    } 

    public class POItem{ 
     private Integer itemID; 
     private String itemCode; 
     private String itemDescription; 
     private Integer itemQuantity; 
    } 

    public class Supplier{ 
     private Integer supplierID; 
     private String supplierName; 
     private String supplierNature; 
     private Address supplierAddress; 
    } 

    public class Address{ 
     private String addressLine1; 
     private String addressLine2; 
     private String addressLine3; 
     private String city; 
     private String state; 
     private string countryCode; 
     private String country; 
     private String postalCode; 
    } 

回答

1

,所以我想你在目標sid上具有相同的對象層次結構e,例如SongDTO,LibraryDTOTrackDTO

然後,你必須聲明的映射方法爲每個對相應對象中的,經由@Mapping將其配置爲根據需要:

public interface MyMapper { 

    @Mapping(source="name", target="title") 
    SongDTO songToDto(Song song); 

    LibraryDTO libraryToDto(Library library); 

    TrackDTO trackToDto(Track track); 
} 

然後例如生成的songToDto()的實現將調用libraryToDto()以將歌曲的庫映射到歌曲DTO的庫DTO中。

同時查看reference guide瞭解更多。

+0

嗨@Gunnar感謝您的回覆,我提出了我的問題,可以幫助我再次對我 – AdamIJK

+0

您是否嘗試了我上面提出的建議? – Gunnar

+0

嗨@Gunnar嗨@Gunnar我嘗試了你的建議,我遵循相同的方式,就像你在上面爲我工作一樣,但我正在嘗試一些不同的場景,因爲面臨困難,我可以給你的郵件地圖添加地圖。我會發送我所有的問題。 – AdamIJK