java 如何使用 Dozer 映射深层(或嵌套)对象属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13424673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:39:51  来源:igfitidea点击:

How to do mapping for deep (or nested) objects properties using Dozer?

javamappingclonejavabeansdozer

提问by supernova

I am having a requirement to map properties of an object which is having nested objects ( deep) to a DTO object. Actually I want to Deflate the complex object to simple DTO with all the properties of even nested objects. I tried BeanUtils but that also don't do deep copy. So I'm exploring Dozer to implement the solution. But even Dozer is mapping first level properties but not deep level nested object properties.

我需要将具有嵌套对象(深)的对象的属性映射到 DTO 对象。实际上,我想将复杂对象缩小为具有甚至嵌套对象的所有属性的简单 DTO。我试过 BeanUtils 但这也不做深复制。所以我正在探索 Dozer 来实施解决方案。但即使是 Dozer 也在映射一级属性而不是深层嵌套对象属性。

Here are my classes :

这是我的课程:

    public class Child1  {
    private String name = "MyName";
    private String address = "ABC";

    public TestDTO dto = new TestDTO("50", "USA");

    public TestDTO getDto() {
        return dto;
    }

    public void setDto(TestDTO dto) {
        this.dto = dto;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }   
}


    public class TestDTO {

    public TestDTO(){}

    public TestDTO(String age, String country) {
        super();
        this.age = age;
        this.country = country;
    }

    private String age = "27";

    private String country = "Canada";

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

}

This is the destination I want do deflate all te properties ( even from nested object TestDTO :

这是我想要放气所有 te 属性的目的地(甚至来自嵌套对象 TestDTO :

    public class TestFacade {

    public String name ;
    public String address;
    private String age ;
    private String country ;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }


}

My mapping file ( I tried wit NO config file also but it was not working so I added a mapping file :

我的映射文件(我也尝试了没有配置文件但它不起作用所以我添加了一个映射文件:

<?xml version="1.0" encoding="UTF-8"?>

<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd">

   <mapping>
    <class-a>Child1</class-a>
    <class-b>TestFacade</class-b>
</mapping>
<mapping>
    <class-a>TestDTO</class-a>
    <class-b>TestFacade</class-b>
    <field>
        <a is-accessible="true">country</a>
        <b is-accessible="true">country</b>
    </field>
    <field>
        <a>age</a>
        <b is-accessible="true">age</b>
    </field>
</mapping>

Finally my Test File :

最后我的测试文件:

  public class QuickTest {


    public static void main(String[] args) {
        Child1 ch = new Child1();
        System.out.println("Get" + ch.getDto().getCountry());


        List myMappingFiles = new ArrayList();
        myMappingFiles.add("dozerMapping.xml");
        DozerBeanMapper mapper = new DozerBeanMapper(myMappingFiles);
        TestFacade f =  mapper.map(ch, TestFacade.class);
        System.out.println(ToStringBuilder.reflectionToString(f));

    }

}

Here is the output:

这是输出:

TestFacade@13adc56[name=MyName,address=ABC,age=<null>,country=<null>]

The age and country properties from nested object are not mapping?

嵌套对象的年龄和国家属性没有映射?

采纳答案by supernova

I found the solution myself...

我自己找到了解决方案...

Contents of my mapping file was wrong. I should use "." notation for nested objects.

我的映射文件的内容是错误的。我应该用“。” 嵌套对象的符号。

so right contents of mapping file :

映射文件的正确内容:

<mapping>
<class-a>Child1</class-a>
<class-b>TestFacade</class-b>
</mapping>
<mapping>
<class-a>TestDTO</class-a>
<class-b>TestFacade</class-b>
<field>
    <a>dto.country</a>
    <b>country</b>
</field>
<field>
    <a>dto.age</a>
    <b >age</b>
</field>

output :

TestFacade@13adc56[name=MyName,address=ABC,age=50,country=USA]

回答by Bhuvan

Accepted answer is incorrect. Here is the correct answer

接受的答案是不正确的。这是正确答案

 <mapping>
    <class-a>Child1</class-a>
    <class-b>TestFacade</class-b>
    <field>
        <a>dto.country</a>
        <b>country</b>
    </field>
    <field>
        <a>dto.age</a>
        <b>age</b>
    </field>
 </mapping>