簡體   English   中英

推土機中的類映射

[英]Class mapping in dozer

我的應用程序中有以下類映射。

 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
}

我必須使用Dozer將源bean轉換為目標bean。 這個用例可行嗎? 我知道推土機默認情況下具有雙向映射,我們可以使其單向。 但是,與將數據從一個bean復制到另一個bean的簡單java代碼相比,編寫單向特定映射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>

用法

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

更多內容 http://dozer.sourceforge.net/documentation/deepmapping.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM