首页 技术 正文
技术 2022年11月20日
0 收藏 632 点赞 4,675 浏览 3661 个字

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918