简体   繁体   English

推土机中的类映射

[英]Class mapping in dozer

I have following class mapping in my application. 我的应用程序中有以下类映射。

 public class Source {
        // Header 
        private String one;
        private String two;
        // Body
        private String three;
        private String four;
        private String five;
        private String six;
        // Getters & Setters
    }
public class Destination {
       private Header header;
       private Body body;
       // Getter & Setter
}
public class Header {
    private String one;
    private String two;
    // Getter & Setters
}
public class Body {
    private String three;
    private String four;
    private String five;
    private String six;
    // Getter & Setters
}

I have to convert from source bean to destination bean using Dozer. 我必须使用Dozer将源bean转换为目标bean。 Is this use-case possible? 这个用例可行吗? I know Dozer has bi-directional mapping by default and we can make it one-way. 我知道推土机默认情况下具有双向映射,我们可以使其单向。 BUT writing that one-way specific mapping xml will be worth the effort as compare to simple java code to copy the data from one bean to another ? 但是,与将数据从一个bean复制到另一个bean的简单java代码相比,编写单向特定映射xml值得付出努力吗? Please suggest. 请提出建议。

Mapping XML 映射XML

<?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 map-id="k" type="one-way">

  <class-a>com.test.pah.Source</class-a>
  <class-b>com.test.pah.Destination</class-b>

  <field>
    <a>one</a>
    <b>header.one</b>
  </field>

  <field>
    <a>two</a>
    <b>header.two</b>
  </field>

  <field>
    <a>three</a>
    <b>body.three</b>
  </field>

   <field>
    <a>four</a>
    <b>body.four</b>
  </field>

   <field>
    <a>five</a>
    <b>body.five</b>
  </field>

   <field>
    <a>six</a>
    <b>body.six</b>
  </field>

 </mapping>
 </mappings>

Usage 用法

List<String> mappingFiles = new ArrayList<String>();

mappingFiles.add("map.xml");

Source source = new Source("1", "2", "3", "4", "5", "6");

DozerBeanMapper mapper = new DozerBeanMapper(mappingFiles);

Destination destination = mapper.map(source, Destination.class, "k");

More Here http://dozer.sourceforge.net/documentation/deepmapping.html 更多内容 http://dozer.sourceforge.net/documentation/deepmapping.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM