将一至多个映射,一行填充dto
有客户实体与实体地址有一对多关系,因此有一个list< address>地址= new ArrayList<>
在Customer.entity地址中具有电子邮件字段。
例如,客户持有一个带有两个元素的地址arraylist。要从您使用的第一个客户那里获得一封电子邮件。getAddress.getAdresses.get(0).getEmail,
因此对于客户ID = 6可以有2个地址,每个地址有一个电子邮件。 构建DTO时,我只需要客户ID和电子邮件。
所以在那种情况下,我想在DTO中两行 6 6
有没有办法在不做任何SQL的情况下使用Maptruct进行操作?喜欢迭代收藏吗?
There is the Customer entity which has a one to many relationship to entity Address,so there is a List<Address> addresses=new ArrayList<>
in Customer.Entity Address has an Email field.
For instance,Customer holds an Address ArrayList with two elements.To get an email from the first customer you do customer.getAddresses.get(0).getEmail
So for Customer ID =6 there can be 2 Addresses with one email per Address.
When building the DTO I just need the Customer Id and the Email.
So in that case I would like two rows in the DTO
6 [email protected]
6 [email protected]
Like doing an SQL query and getting two rows back for the same id.
Is there a way to do that with Mapstruct without doing any sql? Like iterating over a collection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我不确定我是否明白你想要什么。 Mapstruct 用于映射而不是应用逻辑。您描述的情况是我可以使用 SQL 或板载 java 实用程序来解决的问题。
我的方法看起来像这样
I am not sure that i understand what you want. Mapstruct is used for mapping and not applying logic. The case that you describe is something that i would solve eiter with SQL or with onboard java utilities.
My approach looks like this