zoukankan      html  css  js  c++  java
  • Dozer应用——类之间值的映射

    1、 Mappings via Annotation

      

     1 public class SourceBean {
     2 
     3     private Long id;
     4 
     5     private String name;
     6 
     7     @Mapping("binaryData")
     8     private String data;
     9 
    10     @Mapping("pk")
    11     public Long getId() {
    12         return this.id;
    13     }
    14 
    15     public String getName() {
    16         return this.name;
    17     }
    18 }              
    19 public class TargetBean {
    20 
    21     private String pk;
    22 
    23     private String name;
    24 
    25     private String binaryData;
    26 
    27     public void setPk(String pk) {
    28         this.pk = pk;
    29     }
    30 
    31     public void setName(String name) {
    32         this.name = name;
    33     }
    34 }              

    2、Mappings via API

    BeanMappingBuilder builder = new BeanMappingBuilder() {
          protected void configure() {
            mapping(Bean.class, Bean.class,
                    oneWay(),
                    mapId("A"),
                    mapNull(true)
            )
                    .exclude("excluded")
                    .fields("src", "dest",
                            copyByReference(),
                            collectionStrategy(true, 
                                RelationshipType.NON_CUMULATIVE),
                            hintA(String.class),
                            hintB(Integer.class),
                            fieldOneWay(),
                            useMapId("A"),
                            customConverterId("id")
                    )
                    .fields("src", "dest",
                        customConverter("org.dozer.CustomConverter")
                    );
          }
        };
    
    DozerBeanMapper mapper = new DozerBeanMapper();
    mapper.addMapping(builder);

    3、Mappings Via Dozer 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> 
        <class-a>org.dozer.vo.TestObject</class-a>
        <class-b>org.dozer.vo.TestObjectPrime</class-b>   
        <field>
          <a>one</a>
          <b>onePrime</b>
        </field>
      </mapping>  
    
      <mapping wildcard="false"> 
        <class-a>org.dozer.vo.TestObjectFoo</class-a>
        <class-b>org.dozer.vo.TestObjectFooPrime</class-b>   
          <field>
            <a>oneFoo</a>
            <b>oneFooPrime</b>
          </field>
      </mapping>  
    
    </mappings> 

    Mapping Classes

    An example of mapping two classes is defined below. Note: Explicit xml mapping for 2 classes is not required if all the field mapping between src and dest object can be performed by matching on attribute name. Custom xml class mapping is only required when you need to specify any custom field mappings.

    These mappings are bi-directional so you would never need to define an XML map for TestObjectPrime to TestObject. If these two classes had references to complex types that needed type transformation, you would also define them as mappings. Dozer recursively goes through an object and maps everything in it. Data type conversion is performed automatically. Dozer also supports no attribute mappings at all. If supplied two classes that are not mapped, it simply tries to map properties that are the same name.

    Basic Property Mapping

    Implicit Property Mapping (bi-directional)

    Matching field names are automatically handled by Dozer.

    Properties that are of the same name do not need to be specified in the mapping xml file.

    Simple Mappings (bi-directional)

    We will start off simple. If you have two properties with different names they can be mapped as such:

    <field><a>one</a><b>onePrime</b></field>

    Data type conversion

    Data type coversion is performed automatically by the Dozer mapping engine. Currently, Dozer supports the following types of conversions: (these are all bi-directional)

    • Primitive to Primitive Wrapper
    • Primitive to Custom Wrapper
    • Primitive Wrapper to Primitive Wrapper
    • Primitive to Primitive
    • Complex Type to Complex Type
    • String to Primitive
    • String to Primitive Wrapper
    • String to Complex Type if the Complex Type contains a String constructor
    • String to Map
    • Collection to Collection
    • Collection to Array
    • Map to Complex Type
    • Map to Custom Map Type
    • Enum to Enum
    • Each of these can be mapped to one another: java.util.Date, java.sql.Date, java.sql.Time, java.sql.Timestamp, java.util.Calendar, java.util.GregorianCalendar
    • String to any of the supported Date/Calendar Objects.
    • Objects containing a toString() method that produces a long representing time in (ms) to any supported Date/Calendar object.

    Recursive Mapping (bi-directional)

    Dozer supports full Class level mapping recursion. If you have any complex types defined as field level mappings in your object, Dozer will search the mappings file for a Class level mapping between the two Classes that you have mapped. If you do not have any mappings, it will only map fields that are of the same name between the complex types.

    转自:http://dozer.sourceforge.net/documentation/mappingclasses.html

  • 相关阅读:
    针对动态加载方式的C/C++动态链接库编写
    Delphi无法正确动态调用C++ dll库的几个原因
    Windows下VC++显示UTF-8编码中文
    小型C/C++项目的makefile编写
    树状树组(Binary Indexed Tree (BIT))的C++部分实现
    PLSQL创建Oracle定时任务
    Oracle:trunc()函数简介 时间处理及数字小数位处理
    Chrome inspect学习(四)本地环境/测试环境前端与客户端交互,涉及客户端代码报错,如何调试
    Chrome inspect学习(三)如何查看本地环境/测试环境下移动端内嵌H5页面在手机中真实渲染的DOM结构、CSS样式、接口调用
    Chrome inspect学习(二)如何查看线上环境移动端内嵌H5页面在手机中真实渲染的DOM结构、CSS样式、接口调用
  • 原文地址:https://www.cnblogs.com/zhangcybb/p/3627564.html
Copyright © 2011-2022 走看看