推土机深度 属性 映射
Dozer Deep Property Mapping
public class Something{
private string id;
private SomethingElse somethingElse;
private OtherProperties...
}
public class SomethingElse{
private string id;
private OtherProperties...
}
public class SomethingDto{
private string id;
private string somethingElseId;
private OtherProperties...
}
当从某物映射到 SomethingDto 时,我必须做
<mapping>
<class-a>Something</class-a>
<class-b>SomethingDto</class-b>
<field>
<a>somethingElse.id</a>
<b>somethingElse</b>
</field>
</mapping>
我不确定如何做相反的事情,我想要的是 "new SomethingElse" 然后像这样设置 id
public class Something{
private string id;
private SomethingElse somethingElse;
private OtherProperties...
public void setSomethingElseById(String somethingElseId){
somethingElse = new SomethingElse;
somethingElse.setId(somethingElseId);
}
}
<mapping>
<class-a>SomethingDto</class-a>
<class-b>Something</class-b>
<field>
<a>somethingElse</a>
<b set-method='setSomethingElseById(SomethingElse)' >somethingElse</b>
</field>
</mapping>
这看起来确实是一个糟糕的解决方案,所以我尝试了自定义转换器
public class SomethingDtoToSomethingConverter extends DozerConverter<String, SomethingElse> {
public SomethingDtoToSomethingConverter () {
super(String.class, SomethingElse.class);
}
@Override
public SomethingElse convertTo(String source, SomethingElse destination) {
destination = new SomethingElse();
destination.setId(source);
return destination;
}
@Override
public String convertFrom(SomethingElse source, String destination) {
destination = source.getId();
return destination ;
}
}
对于非常简单的事情来说,这看起来像是荒谬的代码量
我怎样才能获得更清洁的解决方案?
您使用的是哪个版本的 Dozer?这应该使用您定义的第一个映射开箱即用,因为推土机映射默认是双向的。
我已经根据您的代码创建了一个示例,并通过了单元测试:
Something.java
public class Something {
private String id;
private SomethingElse somethingElse;
...
}
SomethingElse.java
public class SomethingElse {
private String id;
...
}
SomethingDto.java
public class SomethingDto {
private String id;
private String somethingElseId;
...
}
dozer.xml
<mapping>
<class-a>Something</class-a>
<class-b>SomethingDto</class-b>
<field>
<a>somethingElse.id</a>
<b>somethingElseId</b>
</field>
</mapping>
单元测试
public class DozerMappingTest {
private DozerBeanMapper beanMapper;
@Test
public void sourceToDestination() {
List<String> mappingFiles = new ArrayList<String>();
mappingFiles.add("dozer.xml");
this.beanMapper = new DozerBeanMapper(mappingFiles);
Something source = new Something();
source.setId("A");
SomethingElse somethingElse = new SomethingElse();
somethingElse.setId("B");
source.setSomethingElse(somethingElse);
SomethingDto dest = beanMapper.map(source, SomethingDto.class);
assertEquals("A", dest.getId());
assertEquals("B", dest.getSomethingElseId());
}
@Test
public void destinationToSource() {
List<String> mappingFiles = new ArrayList<String>();
mappingFiles.add("dozer.xml");
this.beanMapper = new DozerBeanMapper(mappingFiles);
SomethingDto source = new SomethingDto();
source.setId("A");
source.setSomethingElseId("B");
Something dest = beanMapper.map(source, Something.class);
assertEquals("A", dest.getId());
assertEquals("B", dest.getSomethingElse().getId());
}
}
public class Something{
private string id;
private SomethingElse somethingElse;
private OtherProperties...
}
public class SomethingElse{
private string id;
private OtherProperties...
}
public class SomethingDto{
private string id;
private string somethingElseId;
private OtherProperties...
}
当从某物映射到 SomethingDto 时,我必须做
<mapping>
<class-a>Something</class-a>
<class-b>SomethingDto</class-b>
<field>
<a>somethingElse.id</a>
<b>somethingElse</b>
</field>
</mapping>
我不确定如何做相反的事情,我想要的是 "new SomethingElse" 然后像这样设置 id
public class Something{
private string id;
private SomethingElse somethingElse;
private OtherProperties...
public void setSomethingElseById(String somethingElseId){
somethingElse = new SomethingElse;
somethingElse.setId(somethingElseId);
}
}
<mapping>
<class-a>SomethingDto</class-a>
<class-b>Something</class-b>
<field>
<a>somethingElse</a>
<b set-method='setSomethingElseById(SomethingElse)' >somethingElse</b>
</field>
</mapping>
这看起来确实是一个糟糕的解决方案,所以我尝试了自定义转换器
public class SomethingDtoToSomethingConverter extends DozerConverter<String, SomethingElse> {
public SomethingDtoToSomethingConverter () {
super(String.class, SomethingElse.class);
}
@Override
public SomethingElse convertTo(String source, SomethingElse destination) {
destination = new SomethingElse();
destination.setId(source);
return destination;
}
@Override
public String convertFrom(SomethingElse source, String destination) {
destination = source.getId();
return destination ;
}
}
对于非常简单的事情来说,这看起来像是荒谬的代码量 我怎样才能获得更清洁的解决方案?
您使用的是哪个版本的 Dozer?这应该使用您定义的第一个映射开箱即用,因为推土机映射默认是双向的。
我已经根据您的代码创建了一个示例,并通过了单元测试:
Something.java
public class Something {
private String id;
private SomethingElse somethingElse;
...
}
SomethingElse.java
public class SomethingElse {
private String id;
...
}
SomethingDto.java
public class SomethingDto {
private String id;
private String somethingElseId;
...
}
dozer.xml
<mapping>
<class-a>Something</class-a>
<class-b>SomethingDto</class-b>
<field>
<a>somethingElse.id</a>
<b>somethingElseId</b>
</field>
</mapping>
单元测试
public class DozerMappingTest {
private DozerBeanMapper beanMapper;
@Test
public void sourceToDestination() {
List<String> mappingFiles = new ArrayList<String>();
mappingFiles.add("dozer.xml");
this.beanMapper = new DozerBeanMapper(mappingFiles);
Something source = new Something();
source.setId("A");
SomethingElse somethingElse = new SomethingElse();
somethingElse.setId("B");
source.setSomethingElse(somethingElse);
SomethingDto dest = beanMapper.map(source, SomethingDto.class);
assertEquals("A", dest.getId());
assertEquals("B", dest.getSomethingElseId());
}
@Test
public void destinationToSource() {
List<String> mappingFiles = new ArrayList<String>();
mappingFiles.add("dozer.xml");
this.beanMapper = new DozerBeanMapper(mappingFiles);
SomethingDto source = new SomethingDto();
source.setId("A");
source.setSomethingElseId("B");
Something dest = beanMapper.map(source, Something.class);
assertEquals("A", dest.getId());
assertEquals("B", dest.getSomethingElse().getId());
}
}