---
title: "Bean Mapping Utils--Dozer初探"
author: "toby941"
date: "2008-06-03T00:57:01Z"
tags: []
categories: ["企业架构"]
slug: "bean-mapping-utils-dozer"
draft: false
lastmod: "2008-06-03T00:57:01Z"
id: 199586
url: "https://toby941.iteye.com/blog/199586"
---

最近从springside3项目里看到个值对象拷贝框架 dozer 学习了一下，发现还是满有用的，写点学习小结

适用情况：分层架构时对DTO，VO，Pojo之间值属性的拷贝，并且这种拷贝可以跨属性的，即两个对象可以完全不一样（字段名称，字段属性）

比如将基本类型转为包装类型，数组转字符串，各种Collection集合类型之间互相转化，举个例子页面有个产品form有个关键词属性 是个String数组，对应页面一排输入框，而在数据库里是用逗号（，）分隔的字符串，一般都是在程序里用

String.split(","),StringUtils.join(String\[]s)这样的方法进行转化,现在将这两个互相转化的方法定义的pojo中

写道

public void setWords(String words) {  
this.keyWords = words.split(",");  
}  
public String getWords(){  
return StringUtils.join(keyWords);  
}

 在相应的dozer-bean-mapping.xml文件中定义get，set方法

写道

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;!DOCTYPE mappings PUBLIC "-//DOZER//DTD MAPPINGS//EN"  
"http://dozer.sourceforge.net/dtd/dozerbeanmapping.dtd"&gt;  
&lt;mappings&gt;  
&lt;configuration&gt;  
&lt;stop-on-errors&gt;true&lt;/stop-on-errors&gt;  
&lt;date-format&gt;yyyy-MM-dd&lt;/date-format&gt;  
&lt;wildcard&gt;true&lt;/wildcard&gt;  
&lt;trim-strings&gt;true&lt;/trim-strings&gt;  
&lt;/configuration&gt;  
&lt;mapping map-null="false"&gt;  
&lt;class-a&gt;com.spring.bean.Students&lt;/class-a&gt;  
&lt;class-b&gt;com.spring.view.command.StudentCommand&lt;/class-b&gt;  
&lt;field&gt;  
&lt;a&gt;word&lt;/a&gt;  
&lt;b set-method="setWords(java.lang.String)" get-method="getWords"&gt;keyWords&lt;/b&gt;  
&lt;/field&gt;  
&lt;/mapping&gt;  
&lt;/mappings&gt;

 测试代码

写道

package com.spring.test;

import static java.lang.System.out;

import java.util.Date;

import net.sf.dozer.util.mapping.DozerBeanMapper;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;  
import org.junit.Test;  
import org.junit.runner.RunWith;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.test.context.ContextConfiguration;  
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.spring.bean.Students;  
import com.spring.view.command.StudentCommand;

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration  
public class BaseTest {

@Autowired  
private DozerBeanMapper dozer;

@Test  
public void fristDozerTetst() {  
Students stu = new Students();  
stu.setAge(" dff ");  
stu.setId("dd");  
stu.setDate(new Date());  
stu.setWord("1,2,3,4,5,6");  
StudentCommand studentCommand = (StudentCommand) dozer.map(stu,  
StudentCommand.class);  
out.println(ReflectionToStringBuilder.toString(studentCommand));  
StudentCommand sc1 = new StudentCommand();  
sc1.setAge("444");  
sc1.setName("");  
sc1.setValue(5555);  
sc1.setDate("2008-08-08");  
sc1.setKeyWords(new String\[] { "a", "b", "c", "d", "e", "f" });  
Students stu1 = (Students) dozer.map(sc1, Students.class);  
out.println(ReflectionToStringBuilder.toString(stu1));  
String s=stu1.getWord();  
out.println(stu1.getWord());  
}

 

控制台输出

写道

com.spring.view.command.StudentCommand@f9c40\[id=dd,name=&lt;null&gt;,age=dff,value=0,date=2008-06-03,keyWords={1,2,3,4,5,6}]  
com.spring.bean.Students@1bb5c09\[id=&lt;null&gt;,name=,age=444,value=5555,order=&lt;null&gt;,date=Fri Aug 08 00:00:00 CST 2008,orderByType=&lt;null&gt;,word=abcdef]  
abcdef

  可见 dozer将原来的字符串"1,2,3,4,5,6"转化为字符数组={1,2,3,4,5,6}

而字符数组{ "a", "b", "c", "d", "e", "f" }则变为word=abcdef；

另外dozer也可格式化时间

&lt;date-format&gt;yyyy-MM-dd&lt;/date-format&gt;  
date=2008-06-03

sc1.setDate("2008-08-08");

date=Fri Aug 08 00:00:00 CST 2008

若两个需要互相拷贝的java对象有相同的字段名，则在配置文件中无需在指出就可以自带的copy，很方便

在程序里再也不用出现一堆的setXXX（XXX.getXXX)了

最后是spring的context文件

写道

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;  
&lt;bean id="net.sf.dozer.util.mapping.MapperIF"  
class="net.sf.dozer.util.mapping.DozerBeanMapper" scope="singleton"&gt;  
&lt;property name="mappingFiles"&gt;  
&lt;list&gt;  
&lt;value&gt;dozer-bean-mapping.xml&lt;/value&gt;  
&lt;/list&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;/beans&gt;