gpt4 book ai didi

spring - Mapstruct 中的嵌套映射

转载 作者:行者123 更新时间:2023-12-04 18:31:03 28 4
gpt4 key购买 nike

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

目标类(class)
这里是我的目标类 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 , 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。

另请查看 reference guide了解更多。

关于spring - Mapstruct 中的嵌套映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39078941/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com