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

时间:2012-11-16 21:29:02

标签: java mapping clone javabeans dozer

我需要将具有嵌套对象(深层)的对象的属性映射到DTO对象。实际上我想将复杂对象与具有甚至嵌套对象的所有属性的简单DTO进行放松。 我尝试了BeanUtils,但也没有做深拷贝。所以我正在探索Dozer来实施解决方案。 但即使是Dozer也在映射第一级属性,而不是深层嵌套对象属性。

以下是我的课程:

    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;
    }

}

这是我想要对所有te属性进行deflate的目的地(甚至来自嵌套对象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;
    }


}

我的映射文件(我试过没有配置文件,但它没有工作,所以我添加了一个映射文件:

<?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>

最后我的测试文件:

  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));

    }

}

这是输出:

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

嵌套对象的年龄和国家/地区属性是否未映射?

2 个答案:

答案 0 :(得分:2)

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

 <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>

答案 1 :(得分:1)

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

我的映射文件的内容错误。我应该用“。”嵌套对象的表示法。

如此正确的映射文件内容:

<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]