【问题标题】:Add ModelMapper Maping from Entity Class to Dto Class with OneToMany relationship将 ModelMapper 映射从实体类添加到具有 OneToMany 关系的 Dto 类
【发布时间】:2022-02-03 18:37:58
【问题描述】:

我有这种关系(Spring Boot JPA 实现):

一个人有多个地址

一个地址包含一个国家

public class Person {
    
private Integer id;
private Integer name;
private Integer age;

//relations
}
  
        
public class Address {
    
private Integer id;
private Integer person_id;
private String street;
private String city;
private String country_id;
private Boolean preferred;

//relations
}
        
public class Country {
    
private Integer id;
private Integer description;

//relations

我想以这种方式返回一个 DTO

public class PersonDto {

 private Integer id;
 private Integer name;
 private Integer age;
 private String street;
 private String city;
 private Integer country //description;

但其实我想要

  1. 只返回人的首选地址(只能有一个)。
  2. 仅获取嵌套类地址中的国家/地区描述

ModelMapper (AddMapping Strategy) 可以实现

谢谢,

【问题讨论】:

    标签: java spring jpa


    【解决方案1】:

    其中一个选项是使用Converter。

    import org.modelmapper.Converter;
    import org.modelmapper.spi.MappingContext;
    
    public class PersonToDtoConverter implements Converter<Person, PersonDto> {
    
      @Override
      public PersonDto convert(MappingContext<Person, PersonDto> context) {
        Person source = context.getSource();
        PersonDto destination = context.getDestination();
        if (destination == null) {
          destination = new PersonDto();
        }
        destination.setId(source.getId());
        destination.setName(source.getName());
        destination.setAge(source.getAge());
        Address address = source.getAddresses().stream().filter(Address::getPreferred).findFirst().orElse(null);
        if (address != null) {
          destination.setStreet(address.getStreet());
          destination.setCity(address.getCity());
          destination.setCountry(address.getCountry().getDescription());
        }
        return destination;
      }
    }
    

    然后你需要将转换器注册到ModelMapper 实例。

    import org.modelmapper.ModelMapper;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Mappings {
    
      public static void main(String[] args) {
        //setup
        Country germany = new Country();
        germany.setId(1);
        germany.setDescription(11);
        Address address = new Address();
        address.setCountry(germany);
        address.setCity("Munich");
        address.setId(1);
        address.setCountry_id("DE");
        address.setStreet("some street");
        address.setPreferred(true);
        address.setPerson_id(1);
        Person person = new Person();
        person.setId(1);
        person.setAge(29);
        person.setName(111);
        List<Address> addresses = new ArrayList<>();
        addresses.add(address);
        person.setAddresses(addresses);
    
        //convert
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.addConverter(new PersonToDtoConverter());
        PersonDto personDto = modelMapper.map(person, PersonDto.class);
        System.out.println(personDto);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-14
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-14
      相关资源
      最近更新 更多