Dozer应用——类之间值的映射
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
Dozer应用——类之间值的映射的更多相关文章
- Hibernate类中集合的映射
1 pojo类集合属性的映射 在pojo类中定义集合属性. 1.1 List集合属性的映射 package org.guangsoft.pojo; import java.util.List; pu ...
- Hibernate基于注解方式配置来实现实体和数据库之间存在某种映射关系
实体和数据库之间存在某种映射关系,hibernate根据这种映射关系完成数据的存取.在程序中这种映射关系由映射文件(*.hbm.xml)或者java注解(@)定义. 本文以java注解的形式总结映射关 ...
- bitMap算法实现以及ckHash函数类,将字符串映射成数字,同时可以将数字映射成字符串
ckHash函数类,将字符串映射成数字,同时可以将数字映射成字符串 说明 1.所谓的BitMap就是用一个bit位来标记某个元素所对应的value,而key即是该元素,由于BitMap使用了bit位来 ...
- oc之封装与类之间的关系
1. 面向对象的特征-封装? 封装: 现实生活中的封装: 将很多的小东西 塞在1个大口袋里面. 好处: a. 对外部屏蔽. b. 方便管理. 代码的封装: 函数/方法 就是1种封装的体现: 将一段代码 ...
- 全面解释java中StringBuilder、StringBuffer、String类之间的关系
StringBuilder.StringBuffer.String类之间的关系 java中String.StringBuffer.StringBuilder是编程中经常使用的字符串类,在上一篇博文中我 ...
- python之路--类与类之间的关系
类和类之间的关系 在我们的世界中事物和事物之间总会有一些联系. 在面向对象中. 类和类之间也可以产生相关的关系 1. 依赖关系 执行某个动作的时候. 需要xxx来帮助你完成这个操作. 此时的关系是最轻 ...
- Day18--Python--面向对象--类与类之间的关系
1. 依赖关系 在方法中引入另一个类的对象 (最不紧密的一种关系) 依赖关系都是把另一个对象当做参数 class Elephant: def __init__(self, name): self.na ...
- UML类图及类与类之间的关系
原文地址:http://www.uml.org.cn/oobject/201211231.asp 类图用于描述系统中所包含的类以及它们之间的相互关系,帮助人们简化对系统的理解,它是系统分析和设计阶段的 ...
- python学习 day017打卡 类与类之间的关系
本节主要的内容: 1.依赖关系 2.关联关系,组合关系,聚合关系 3.继承关系,self到底是什么? 4.类中的特殊成员 一.类与类之间的依赖关系 在面向对象的世界中,类与类中存在以下关系: 1.依赖 ...
随机推荐
- internet协议
internet协议入门 前言 劳于读书,逸于作文. 原文地址:internet协议入门 博主博客地址:Damonare的个人博客 博主之前写过一篇博客:网络协议分析,在这篇博客里通过抓包,具体的分析 ...
- 服务器端spice配置详解
1. 安装必要的工具 sudo apt-get install build-essential autoconf git-core intltool 2. 安装必要的依赖包 -dev libxfixe ...
- adb shell出现error错误
模拟器已打开,原因在于端口被占用. 找到kadb.exe 进程,结束进程!
- linux源代码阅读笔记 get_free_page()代码分析
/* 34 * Get physical address of first (actually last :-) free page, and mark it 35 * used. If no fre ...
- HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)
想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...
- 如何防止通过IP地址访问Tomcat管理页面
方法:建议修改webapps下面的原始文件夹的名称,比如加一个后缀: 当需要用管理页面的时候,可以将含有manager的文件夹的后缀去掉即可 manager和host-manager共2个文件夹
- URAL 1073 Square Country(DP)
题目链接 题意 :这个人要投资地,每块地都是正方形并且边长都是整数,他希望他要买的地尽量的少碎块.每买一块地要付的钱是边长的平方,而且会得到一个一份证书,给你一个钱数,让你求出能得到的证书个数. 思路 ...
- [主席树]ZOJ2112 && BZOJ1901 Dynamic Rankings
题意:n个数,q个询问 (n<=50000, q<=10000) Q x y z 代表询问[x, y]区间里的第z小的数 C x y 代表将(从左往右数)第x个数变成y 上篇介绍了在 ...
- cojs 火龙果 解题报告
昨天晚上做了一发HNOI,感觉有很多新的idea 于是就选了一个出成题目辣 我们考虑暴力 暴力很明显是把这个图A<=D,B<=E的形态搞出来 之后处理相关的询问 这样我们会很容易得到正解: ...
- static int和static final int的区别
1.static变量 按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是没有被static修饰的变量,叫实例变量.两者的区别是: 对于静态变量在内 ...