Dozer使用简记

什么是Dozer

Dozer是一个Java对象转换工具,可以在JavaBean和JavaBean之间进行递归数据复制,并且适应不同复杂的类型。Dozer会直接将名称相同的属性进行复制,属性名不同或者有特殊的要求则可以在xml中进行配置。

准备使用Dozer

在pom.xml文件中添加以下配置

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/net.sf.dozer/dozer -->
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.5.1</version>
</dependency>
待转换JavaBean

SourceObject

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class Company implements Serializable {
private long id;
private String name;
private String code;
private int type;
private int order;
private Date createTime;
private List<Long> storageList;
public Company(long id, String name, String code, int type, int order, Date createTime) {
this.id = id;
this.name = name;
this.code = code;
this.type = type;
this.order = order;
this.createTime = createTime;
this.storageList = Lists.newArrayList();
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public String getCode() {
return code;
}
public int getType() {
return type;
}
public int getOrder() {
return order;
}
public Date getCreateTime() {
return createTime;
}
public List<Long> getStorageList() {
return storageList;
}
public void setStorageList(List<Long> storageList) {
this.storageList = storageList;
}
}

DestinationObject

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class CompanyVo {
private long id;
private String name;
private String code;
private int types;
private String order;
private Date time;
private List<Long> storageList;
public void setId(long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setCode(String code) {
this.code = code;
}
public void setTypes(int types) {
this.types = types;
}
public void setOrder(String order) {
this.order = order;
}
public void setStorageList(List<Long> storageList) {
this.storageList = storageList;
}
public List<Long> getStorageList() {
return storageList;
}
public void setTime(Date time) {
this.time = time;
}
@Override
public String toString() {
return "CompanyVo{" +
"id=" + id +
", name='" + name + '\'' +
", code='" + code + '\'' +
", types=" + types +
", order='" + order + '\'' +
", time=" + time +
", storageList=" + storageList +
'}';
}
}
对象转换
1
2
3
4
5
6
7
8
9
10
11
public class Main {
public static void main(String[] args){
Mapper mapper = new DozerBeanMapper();
Company company=new Company(100L, "NET", "1001", 3, 5,new Date());
company.getStorageList().add(125L);
company.getStorageList().add(130L);
company.getStorageList().add(110L);
CompanyVo companyVo=mapper.map(company,CompanyVo.class);
System.out.println(companyVo);
}
}
运行结果
1
CompanyVo{id=100, name='NET', code='1001', types=0, order='5', time=null, storageList=[125, 130, 110]}

观察运行结果,会发现有两个属性并没有正确复制,因为两个JavaBean对应的属性名不同。

xml配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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 date-format="yyyy-MM-dd">
<class-a>com.net.meta.Company</class-a>
<class-b>com.net.vo.CompanyVo</class-b>
<field>
<a>type</a>
<b>types</b>
</field>
<field>
<a>createTime</a>
<b>time</b>
</field>
</mapping>
</mappings>

新建dozer.xml配置文件,配置两个JavaBean不同属性名之间的映射关系。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void main(String[] args){
Company company=new Company(100L, "NET", "1001", 3, 5,new Date());
company.getStorageList().add(125L);
company.getStorageList().add(130L);
company.getStorageList().add(110L);
DozerBeanMapper mapper=new DozerBeanMapper();
List mapFiles= Lists.newArrayList();
mapFiles.add("dozer.xml");
mapper.setMappingFiles(mapFiles);
CompanyVo companyVo=mapper.map(company,CompanyVo.class);
System.out.println(companyVo);
}
运行结果
1
CompanyVo{id=100, name='NET', code='1001', types=3, order='5', time=Wed Aug 17 18:47:08 CST 2016, storageList=[125, 130, 110]}
注意事项

1:

1
DestinationObject destObject = mapper.map(sourceObject, DestinationObject.class);

DestinationObject必须有无参构造方法,不然会报java.lang.NoSuchMethodException: com.net.vo.CompanyVo.<init>()错误。

2:

SourceObject和DestinationObject中属性必须可读写,比如提供公开的get或set方法,不然无法复制数据。

1
CompanyVo{id=0, name='null', code='null', types=0, order='null', createTime=null, storageList=null}

3:

dozer正常输出日志需要增加两个jar包

slf4j-api和slf4j-simple,或者,slf4j-api和slf4j-log4j12

例如:

1
2
3
4
5
6
7
8
9
10
11
12
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.1</version>
</dependency>
参考

https://github.com/DozerMapper/dozer/wiki/Getting-Started

热评文章