1、 Mappings via Annotation

  

 public class SourceBean {

     private Long id;

     private String name;

     @Mapping("binaryData")
private String data; @Mapping("pk")
public Long getId() {
return this.id;
} public String getName() {
return this.name;
}
}
public class TargetBean { private String pk; private String name; private String binaryData; public void setPk(String pk) {
this.pk = pk;
} public void setName(String name) {
this.name = name;
}
}

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

最新文章

  1. 使用python的redis 实现消息的pub/sub功能
  2. Jackson序列化和反序列化Json数据完整示例
  3. 夺命雷公狗---node.js---13之Buffer的转换
  4. JDBC项目实践
  5. 发布网站,报Access to the path is denied的解决办法
  6. 使用java的Calendar对象获得当前日期的上几个度开始、结束时间
  7. English words
  8. js 实现图片压缩并转换成base64(data:image/jpeg;base64)格式
  9. Android - 多语言自动适配
  10. 安徽省2016“京胜杯”程序设计大赛_G_木条染色
  11. 关于java中,json字符串转集合和对象,或者集合转json字符串的解决方法
  12. Nodejs+Grunt配置SASS项目自动编译
  13. Spring Boot 2.0 配置图文教程
  14. window注册鼠标右键菜单,及子菜单
  15. 牛客OI周赛7-提高组 A 小睿睿的等式
  16. CAS和ABA问题
  17. Cocos Creator的类别
  18. 使用rownum对oracle分页【原】
  19. java 测量运行时间 单位:毫秒
  20. mac下搭建node+koa2项目

热门文章

  1. [ios]ios的延迟执行方法
  2. java 产生随机数的方法
  3. MongoDB 性能优化五个简单步骤
  4. 【redis】04set类型和zset类型
  5. POJ 2299 Ultra-QuickSort (排序+数据离散化+求顺序数)
  6. GET和POST的区别,就是明信片和信封的区别
  7. SDUT2142数据结构实验之图论二:基于邻接表的广度优先搜索遍历
  8. 简单易懂的现代魔法&mdash;&mdash;Play Framework攻略2
  9. hdu 2837 Calculation
  10. 李洪强iOS开发之【Objective-C】09-空指针和野指针