Mapstruct中的嵌套映射 [英] Nested Mapping in Mapstruct

查看:648
本文介绍了Mapstruct中的嵌套映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MapStruct API的新手,任何人都可以说如何做嵌套映射. 我有两个类,一个是我的实际purchaseOrder类,即我的目标类,另一个是EDPurchaseOrder类,即源文件,这里不必担心我使用的命名约定,只需处理源文件和目标文件即可./p>

源类
源类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;
    }

解决方案

所以我假设您在目标端具有相同的对象层次结构,例如SongDTOLibraryDTOTrackDTO.

然后,您必须为每对对应对象声明一个映射方法,并根据需要通过@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.

还可以查看参考指南以进行学习更多.

I am new to MapStruct API, can anyone say how to do nested Mapping. I have two classes one is my actual purchaseOrder class, which is known my target class, the other is EDPurchaseOrder class which known as source file, here don't worry about the naming conventions I used, just go with source and target files.

Source Classes
Source class EDCustomerOrder and its reference classes

    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;
    }

Target classes
Here my target class CustomerOrder and its reference classes

    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;
    }

解决方案

So I suppose you have the same hierarchy of objects on the target side, e.g. a SongDTO, LibraryDTO and TrackDTO.

Then you'd have to declare a mapping method for each of those pairs of corresponding objects, configuring it via @Mapping as needed:

public interface MyMapper {

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

    LibraryDTO libraryToDto(Library library);

    TrackDTO trackToDto(Track track);
}

Then e.g. the generated implementation of songToDto() will invoke libraryToDto() in order to map the song's library into the song DTO's library DTO.

Also check out the reference guide to learn more.

这篇关于Mapstruct中的嵌套映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆