将一至多个映射,一行填充dto

发布于 01-19 20:34 字数 636 浏览 1 评论 0原文

有客户实体与实体地址有一对多关系,因此有一个list< address>地址= new ArrayList<>在Customer.entity地址中具有电子邮件字段。

例如,客户持有一个带有两个元素的地址arraylist。要从您使用的第一个客户那里获得一封电子邮件。getAddress.getAdresses.get(0).getEmail,

因此对于客户ID = 6可以有2个地址,每个地址有一个电子邮件。 构建DTO时,我只需要客户ID和电子邮件。

所以在那种情况下,我想在DTO中两行 6 6

相同的ID。

有没有办法在不做任何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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦回梦里2025-01-26 20:34:07

我不确定我是否明白你想要什么。 Mapstruct 用于映射而不是应用逻辑。您描述的情况是我可以使用 SQL 或板载 java 实用程序来解决的问题。

我的方法看起来像这样

package mapping;

import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class MappingMain {
    public static void main(String[] args) {
        Customer customer = Customer.
                builder()
                .id(1)
                .name("John")
                .adresses(Arrays.asList(                                          
Address.builder().email("[email protected]").street("exampleStreet1").build(),                        
Address.builder().email("[email protected]").street("exampleStreet2").build()))
                .build();

    //single customer
    List<ResultingDTO> resultingDTOS =
            customer.getAdresses()
                    .stream()
                    .map(Address::getEmail)
                    .map(email ->
                            ResultingDTO.builder().email(email).id(customer.getId()).build()
                        )
                    .collect(Collectors.toList());
    System.out.println(resultingDTOS);


    //multiple customer "select" by id
    List<Customer> listOfCustomers = Arrays.asList(customer, Customer.builder().id(33).build());

    List<ResultingDTO> resultList = new ArrayList<>();
    listOfCustomers
            .stream()
            .filter(singleCustomer -> singleCustomer.getId() == 1)
            .forEach(customerWithId_1 -> {
                resultList.addAll(customerWithId_1.getAdresses()
                        .stream()
                        .map(Address::getEmail)
                        .map(email ->
                                ResultingDTO.builder().email(email).id(customer.getId()).build()
                        )
                        .collect(Collectors.toList()));
            });
    System.out.println(resultList);

    }

    @Data
    @Builder
    public static class Customer {
        private int id;
        private String name;
        private List<Address> adresses;
    }

    @Data
    @Builder
    public static class Address {
        private String street;
        private String email;
    }

    @Data
    @Builder
    public static class ResultingDTO {
        private int id;
        private String email;
    }


}

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

package mapping;

import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class MappingMain {
    public static void main(String[] args) {
        Customer customer = Customer.
                builder()
                .id(1)
                .name("John")
                .adresses(Arrays.asList(                                          
Address.builder().email("[email protected]").street("exampleStreet1").build(),                        
Address.builder().email("[email protected]").street("exampleStreet2").build()))
                .build();

    //single customer
    List<ResultingDTO> resultingDTOS =
            customer.getAdresses()
                    .stream()
                    .map(Address::getEmail)
                    .map(email ->
                            ResultingDTO.builder().email(email).id(customer.getId()).build()
                        )
                    .collect(Collectors.toList());
    System.out.println(resultingDTOS);


    //multiple customer "select" by id
    List<Customer> listOfCustomers = Arrays.asList(customer, Customer.builder().id(33).build());

    List<ResultingDTO> resultList = new ArrayList<>();
    listOfCustomers
            .stream()
            .filter(singleCustomer -> singleCustomer.getId() == 1)
            .forEach(customerWithId_1 -> {
                resultList.addAll(customerWithId_1.getAdresses()
                        .stream()
                        .map(Address::getEmail)
                        .map(email ->
                                ResultingDTO.builder().email(email).id(customer.getId()).build()
                        )
                        .collect(Collectors.toList()));
            });
    System.out.println(resultList);

    }

    @Data
    @Builder
    public static class Customer {
        private int id;
        private String name;
        private List<Address> adresses;
    }

    @Data
    @Builder
    public static class Address {
        private String street;
        private String email;
    }

    @Data
    @Builder
    public static class ResultingDTO {
        private int id;
        private String email;
    }


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文