英文:
How Map two related Entities to one DTO Object using ModelMapper
问题
我在我的应用程序中有两个相关的实体:integratorDetails
(集成商详细信息)和 IntegratorChannelDetails
(集成商通道详细信息)。我想要实现的目标是使用 ModelMapper 将 integratorDetails
和 IntegratorChannelDetails
映射到一个 DTO 对象 IntegratorAllInfoDto
,该 DTO 对象具有与这些实体类似的字段。但是我不确定如何做,以下是这些实体类的代码示例:
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
:
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;
}
我的 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;
}
在这里,我已经为你提供了这些类的代码示例。如果你需要进一步的帮助,可以告诉我你遇到的具体问题。
英文:
I have two related Entities in my application 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
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
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
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;
}
答案1
得分: 2
如果您需要将多个对象映射到单个目标,可以这样做。
ModelMapper modelMapper = new ModelMapper();
IntegratorDTO dto = modelMapper.map(details, IntegratorDTO.class);
// 这将向 dto 添加附加值。
modelMapper.map(integratorChannelDetails, dto);
英文:
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);
答案2
得分: 0
就像文档中所示http://modelmapper.org/getting-started/
您可以在DTO上连接实体的名称,例如:
源模型
// 假设每个类上都有getter和setter
class Order {
Customer customer;
Address billingAddress;
}
class Customer {
Name name;
}
class Name {
String firstName;
String lastName;
}
class Address {
String street;
String city;
}
目标模型
// 假设有getter和setter
class OrderDTO {
String customerFirstName;
String customerLastName;
String billingStreet;
String billingCity;
}
英文:
Like the documentation http://modelmapper.org/getting-started/
You can concat the names of entities on DTO, like:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论