• 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

  • 相关阅读:
    IOS 获取时间的问题
    JQ 点赞
    截取字符串第一个空格前的字符
    JQ 的一些方法
    匿名函数var a =function(){}和function a(){}的区别
    完美支持中文编程的 Emacs 配置文件 .emacs
    linux find 命令忽略某个或多个子目录的方法
    Linux下如何用date设置时间
    nagios的监控代码!
    shell脚本监控apache进程数和oracle连接数
  • 原文地址:https://www.cnblogs.com/zhangcybb/p/3627564.html
Copyright © 2020-2023  润新知