如何在模型映射器中跳过目标源中的属性?
How to skip properties in destination source in modelmapper?
我有两个类。 RequestDTO 和实体。我想将 RequestDTO 映射到实体。在那种情况下,我想手动插入实体 属性 之一,这意味着 属性 不在请求 DTO 中。如何使用 modelmapper 实现此目的。
public class RequestDTO {
private String priceType;
private String batchType;
}
public class Entity {
private long id;
private String priceType;
private String batchType;
}
Entity newEntity = modelMapper.map(requestDto, Entity.class);
但这不起作用,它说它不能将字符串转换为长。我请求解决这个问题或更好的方法。
如果您想手动执行映射(理想情况下用于不同的对象)
您可以查看不同对象映射的文档 Property Mapping、
You can define a property mapping by using method references to match
a source getter and destination setter.
typeMap.addMapping(Source::getFirstName, Destination::setName);
The source and destination types do not need to match.
typeMap.addMapping(Source::getAge, Destination::setAgeString);
如果您不想逐个字段进行映射以避免样板代码
您可以配置一个跳过映射器,以避免将某些字段映射到您的目标模型:
modelMapper.addMappings(mapper -> mapper.skip(Entity::setId));
我已经为你的案例创建了一个测试,并且映射适用于双方 无需配置任何东西 :
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Before;
import org.junit.Test;
import org.modelmapper.ModelMapper;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
public class ModelMapperTest {
private ModelMapper modelMapper;
@Before
public void beforeTest() {
this.modelMapper = new ModelMapper();
}
@Test
public void fromSourceToDestination() {
Source source = new Source(1L, "Hello");
Destination destination = modelMapper.map(source, Destination.class);
assertNotNull(destination);
assertEquals("Hello", destination.getName());
}
@Test
public void fromDestinationToSource() {
Destination destination = new Destination("olleH");
Source source = modelMapper.map(destination, Source.class);
assertNotNull(source);
assertEquals("olleH", destination.getName());
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class Source {
private Long id;
private String name;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class Destination {
private String name;
}
我有两个类。 RequestDTO 和实体。我想将 RequestDTO 映射到实体。在那种情况下,我想手动插入实体 属性 之一,这意味着 属性 不在请求 DTO 中。如何使用 modelmapper 实现此目的。
public class RequestDTO {
private String priceType;
private String batchType;
}
public class Entity {
private long id;
private String priceType;
private String batchType;
}
Entity newEntity = modelMapper.map(requestDto, Entity.class);
但这不起作用,它说它不能将字符串转换为长。我请求解决这个问题或更好的方法。
如果您想手动执行映射(理想情况下用于不同的对象)
您可以查看不同对象映射的文档 Property Mapping、
You can define a property mapping by using method references to match a source getter and destination setter.
typeMap.addMapping(Source::getFirstName, Destination::setName);
The source and destination types do not need to match.
typeMap.addMapping(Source::getAge, Destination::setAgeString);
如果您不想逐个字段进行映射以避免样板代码
您可以配置一个跳过映射器,以避免将某些字段映射到您的目标模型:
modelMapper.addMappings(mapper -> mapper.skip(Entity::setId));
我已经为你的案例创建了一个测试,并且映射适用于双方 无需配置任何东西 :
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Before;
import org.junit.Test;
import org.modelmapper.ModelMapper;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
public class ModelMapperTest {
private ModelMapper modelMapper;
@Before
public void beforeTest() {
this.modelMapper = new ModelMapper();
}
@Test
public void fromSourceToDestination() {
Source source = new Source(1L, "Hello");
Destination destination = modelMapper.map(source, Destination.class);
assertNotNull(destination);
assertEquals("Hello", destination.getName());
}
@Test
public void fromDestinationToSource() {
Destination destination = new Destination("olleH");
Source source = modelMapper.map(destination, Source.class);
assertNotNull(source);
assertEquals("olleH", destination.getName());
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class Source {
private Long id;
private String name;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class Destination {
private String name;
}