我是 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;
}
所以我想你在目标端有相同的对象层次结构,例如
SongDTO
、LibraryDTO
和 TrackDTO
。
然后,您必须为每对相应对象声明一个映射方法,并根据需要通过
@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 中。
另请查看参考指南以了解更多信息。
@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;
}