11

我是 MapStruct API 的新手,任何人都可以说如何进行嵌套映射。我有两个类,一个是我实际的 purchaseOrder 类,它被称为我的目标类,另一个是 EDPurchaseOrder 类,它被称为源文件,这里不用担心我使用的命名约定,只需使用源文件和目标文件即可。

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

2 回答 2

13

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

另请查看参考指南以了解更多信息。

于 2016-08-23T08:07:38.890 回答
-1
@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;
    }
于 2020-12-28T12:31:23.303 回答