[英]How Map two related Entities to one DTO Object using ModelMapper
I have two related Entities in my application integratorDetails, IntegratorChannelDetails.我的应用程序integratorDetails IntegratorChannelDetails 中有两个相关实体。 What I want to achieve is to map integratorDetails and IntegratorChannelDetails to a DTO Object IntegratorAllInfoDto which has similar fields as the entities, using ModelMapper, but I am not sure how to do that, below are the entities
我想要实现的是使用 ModelMapper 将 integratorDetails 和 IntegratorChannelDetails 映射到具有与实体相似字段的 DTO 对象 IntegratorAllInfoDto,但我不知道该怎么做,下面是实体
integratorDetails积分器详情
import com.couchbase.client.java.repository.annotation.Field;
import com.couchbase.client.java.repository.annotation.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.couchbase.core.mapping.Document;
import java.util.Date;
import java.util.List;
@Document
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class IntegratorDetails {
@Id
private String integratorId;
@Field
private String name;
@Field
private String accountId;
@Field
private String status;
private String privateKey;
private String publicKey;
private List<ThirdPartyKey> thirdPartyKey;
private Date createdTime;
}
IntegratorChannelDetails IntegratorChannel详情
import com.couchbase.client.java.repository.annotation.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.couchbase.core.mapping.Document;
import java.util.List;
@Document
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class IntegratorChannelDetails {
@Id
private String integratorChannelid;
private String accountId;
private String type;
private List<ChannelType> channelTypes;
private List<ChannelList> channelList;
private List<String> fixedChannels;
private String timeServiceUrl;
private List<RibbonRules> ribbonRules;
int numberOfSlots=4;
}
And my Dto is我的 Dto 是
import com.tdchannels.admin.ms.channel.db.entity.ChannelList;
import com.tdchannels.admin.ms.channel.db.entity.RibbonRules;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
import java.util.List;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class IntegratorAllInfoDto<T> {
private String integratorId;
private String name;
private String accountId;
private String status;
private Date createdTime;
private List<ChannelTypeDto> channelTypes;
private List<ChannelList> channelList;
private List<String> fixedSlots;
private String publicKey;
private List<ThirdPartyKeyDto> thirdPartyKey;
private List<RibbonRules> ribbonRules;
}
If you need to map multible objects into a single destination you do like this.如果您需要将多个对象映射到单个目的地,您可以这样做。
ModelMapper modelMapper = new ModelMapper();
IntegratorDTO dto= modelMapper.map(details, IntegratorDTO.class);
//This will add additional values to the dto.
modelMapper.map(integratorChannelDetails, dto);
Like the documentation http://modelmapper.org/getting-started/像文档http://modelmapper.org/getting-started/
You can concat the names of entities on DTO, like:您可以在 DTO 上连接实体的名称,例如:
Source model
// Assume getters and setters on each class
class Order {
Customer customer;
Address billingAddress;
}
class Customer {
Name name;
}
class Name {
String firstName;
String lastName;
}
class Address {
String street;
String city;
}
Destination Model
// Assume getters and setters
class OrderDTO {
String customerFirstName;
String customerLastName;
String billingStreet;
String billingCity;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.