[英]Add ModelMapper Maping from Entity Class to Dto Class with OneToMany relationship
我有这种关系(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;
但事实上我想要
ModelMapper (AddMapping Strategy) 可以实现
谢谢,
您可以手动定义包含任意逻辑的映射,例如:
ModelMapper mm = new ModelMapper();
mm.typeMap(Person.class, PersonDto.class).addMappings(mapper -> {
mapper.map(
person -> { // the source object
for(Address address : person.getAddresses()){
if(address.isPreferred() && address.getCountry() != null)
return address.getCountry.getDescription();
}
return null; // or default country if no address is preferred / etc...
},
PersonDto::setCountry); // corresponding setter of the DTO
});
var myDto = mm.map(myPerson, PersonDto.class);
一种选择是使用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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.