【问题标题】:Nested Mapping in MapstructMapstruct 中的嵌套映射
【发布时间】:2016-08-22 11:56:26
【问题描述】:

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

【问题讨论】:

    标签: spring mapstruct


    【解决方案1】:

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

    【讨论】:

    • 嗨@Gunnar 感谢您的回复,我完善了我的问题,您能再次帮助我吗
    • 嗨@Gunnar 我尝试了你的建议,我正在遵循与你上面给出的相同的建议,但我正在尝试一些不同的场景来映射我的课程,你能给我吗?你的邮件ID?我会发送我所有的问题。
    • 请将您的问题发布在 StackOverflow 上,以便其他人也可以从答案中学习。但我建议查看参考指南,因为它详细描述了各种映射选项。因此,您很可能会在那里找到答案。谢谢。
    • 根据您的建议,我添加了完整的场景,请帮助我。提前致谢
    • 这个答案很难用它的例子来理解。问题中的类名不适合答案中的类名。问题中没有 songDto 或 trackToDto(还有吗?)。可以编辑吗?
    【解决方案2】:
    @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;
        }
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    猜你喜欢
    • 2023-03-29
    • 2020-11-16
    • 2018-09-15
    • 2019-07-13
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多