我是MapStruct API的新手,任何人都可以说如何进行嵌套Mapping。 我有两个类,一个是我的实际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;
}
答案 0 :(得分:4)
所以我想你在目标方面拥有相同的对象层次结构,例如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中。
另请查看reference guide了解详情。
答案 1 :(得分:-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;
}