简体   繁体   中英

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. 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 ? Please suggest.

Mapping 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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