2016-08-22 748 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