如何使用ModelMapper将两个相关实体映射到一个DTO对象中

huangapple go评论72阅读模式
英文:

How Map two related Entities to one DTO Object using ModelMapper

问题

我在我的应用程序中有两个相关的实体:integratorDetails(集成商详细信息)和 IntegratorChannelDetails(集成商通道详细信息)。我想要实现的目标是使用 ModelMapper 将 integratorDetailsIntegratorChannelDetails 映射到一个 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&lt;ThirdPartyKey&gt; 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&lt;ChannelType&gt; channelTypes;
	private List&lt;ChannelList&gt; channelList;
	private List&lt;String&gt; fixedChannels;
	private String timeServiceUrl;
	private List&lt;RibbonRules&gt; 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&lt;T&gt; {

	private String integratorId;
	private String name;
	private String accountId;
	private String status;
	private Date createdTime;
	private List&lt;ChannelTypeDto&gt; channelTypes;
	private List&lt;ChannelList&gt; channelList;
	private List&lt;String&gt; fixedSlots;
	private String publicKey;
	private List&lt;ThirdPartyKeyDto&gt; thirdPartyKey;
	private List&lt;RibbonRules&gt; 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;
}

huangapple
  • 本文由 发表于 2020年9月24日 17:20:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64043389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定