Mapstruct 中的嵌套映射

Nested Mapping in Mapstruct

我是 MapStruct 的新手 API,谁能告诉我如何进行嵌套映射。 我有两个 classes 一个是我实际的 purchaseOrder class,这是我的目标 class,另一个是 EDPurchaseOrder class,它被称为源文件,这里不要担心我使用的命名约定,只需使用源文件和目标文件即可。

来源类
来源 class EDCustomerOrder 及其引用 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;
    }

目标 classes
这是我的目标 class CustomerOrder 及其参考 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;
    }

所以我想你在目标端有相同的对象层次结构,例如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。

另请查看 reference guide 以了解更多信息。

@Mapper(componentModel = "spring",unmappedTargetPolicy = ReportingPolicy.IGNORE)
    public interface CandidateProfessionalEntityAndDTOMapper {
        @Mappings({
            @Mapping(source = "company.companyId", target = "companyId"),
        })
        Clazz1
            entityToReferencesMapping(Clazz2 entity);
    }
    public class Clazz2 {
        private String companyName;
        private Company company;
    }
    public class Company{
        Integer companyId;
    }
    public class Clazz1 {
       private String companyId;
       private String companyName;
    }