org.dozer.spring boot dozer.dozerbeanmapperfactorybean 属于哪个包

使用Dozer优雅的将DO转换成VO - ImportNew
| 标签: ,
什么是DO、DTO和VO
在中介绍过Java中的各种模型概念。
在这里简单再总结一下:
在日常的项目开发中,VO对应于页面上需要显示的数据(表单),DO对应于数据库中存储的数据(数据表),DTO对应于除二者之外需要进行传递的数据。
很多人可能对VO和DTO并不是那么熟悉,相反对DO却比较熟悉,那是因为在很多项目中由于种种原因我们只使用了DO,原因可能有以下几种:
1、项目太小,对于一种业务实体,封装成一个DO就够了。
2、并不熟悉DTO、VO,更不知道他们之间的区别。
3、了解DO\DTO\VO之间的区别,但是懒得用。
那么,这里,博主再啰嗦一下为什么要引入这么多概念,为什么我要建议大家在自己的项目中使用这些实体对象。
为什么不能只用一个DO
我们来看这样一个例子。假如我们的项目中由User这样一个实体。我们在创建User表的时候一般包含一下属性:
针对这个实体,我们通常需要创建一个DO类,用来封装这个User实体。
public class UserDO {
//唯一主键
private Date createdT
//创建时间
private Date updatedT
//最后更新时间
private String nickN
private String politicalS
//政治面貌,1表示群众,2表示团员,3表示党员,4表示其他,100表示未知
private Integer companyId;
//公司的ID
//数据状态,1表示可用,0表示不可用
//setter and getter
然后,在代码中,从DAO一直到前端展示,我们都通过这个UserDO类的对象来进行数据传输。这样做会有什么问题嘛?
不需要的字段也会传递到前端页面。
如password、createdTime、updatedTime和status这几个字段我们可能在前端根本不需要展示,但是这些字段有可能也会被传递到前端(除非我们在SQL查询的时候没有查询出对应的字段)。这不仅使数据的传输量增大,还可能有安全性问题。
某些字段需要转换,但是无法支持。
对于上面例子中的政治面貌字段,我们在数据库中存储的是数字,但是在前端页面我要展示的是中文描述。这种情况只能在前端通过if/else的方式来分情况展示。
某些字段要展示,但是并不希望出现在数据库中
在User表中我们只保存了这个用户的companyId,需要同时查询company表来查询出该公司的更多详细信息。对于User对象,如果我们想在前端同时展示他所属的公司,希望通过一次查询全都查出来怎么办?有几个简单的方案,第一个是让UserDO中包含一个Company类的属性,通过这个属性来传递。另外一种是把我们希望传到前端的Company的属性也写到UserDO中。但是,如果真的这么做了,那UserDO还能被称作DO了吗?
还有很多问题,这这里就不详细介绍了。
可见,使用一个DO从头用到尾(从数据库到前端页面)并不是一种好的设计。
如何正确的使用DO、DTO、VO
对于上面的例子,我们可以将他设计成以下类。由于模型并不复杂,这里只需要再引入VO就可以了。
UserDO已经和数据库字段一一对应了,这里不需要修改。
public class UserDO {
//唯一主键
private Date createdT
//创建时间
private Date updatedT
//最后更新时间
private String nickN
private String politicalS
//政治面貌,1表示群众,2表示团员,3表示党员,4表示其他,100表示未知
private Integer companyId;
//公司的ID
//数据状态,1表示可用,0表示不可用
//setter and getter
引入UserVO,用于封装传递到前端需要展示的字段。
public class UserVO {
//唯一主键
private String nickN
private String politicalS
//政治面貌,群众、团员、党员等
//setter and getter
UserVO中只包含了展示所需要的字段,并不需要展示的字段在这里不需要包含。
在引入了这个类之后,我们就可在进行数据库查询的时候使用UserDO,然后再需要传递到前端的时候把DO转换成VO。
看到这里,你可能已经发现,UserDO和UserVO中包含了大量的相同字段。难道真的有必要再单独设计个VO嘛?我可以明确告诉你的是,当你的系统越来越大,表中的字段越来越多的时候,使用DO\DTO\VO等概念进行分层处理是绝对有好处的。至于如何进行有效的在不同的实体类间进行转换是我接下来要介绍的。
优雅的将DO转换成VO
是一个对象转换工具。
Dozer可以在JavaBean到JavaBean之间进行递归数据复制,并且这些JavaBean可以是不同的复杂的类型。
所有的mapping,Dozer将会很直接的将名称相同的fields进行复制,如果field名不同,或者有特别的对应要求,则可以在xml中进行定义。
前面我们介绍的DO\DTO\VO不就是JavaBean嘛。正好可以使用Dozer进行转换呀。
除了使用Dozer,当然你还由其他选择:
典型的解决方案就是手动拷贝,弊端很明显,代码中充斥大量Set 和Get方法,真正的业务被埋藏值与值的拷贝之中。
另一种方案就是使用BeanUtil,但BeanUtil不够很好的灵活性,又时候还不得不手动拷贝。Dozer可以灵活的对对象进行转换,且使用简单。
Dozer 支持的转换类型
Primitive 基本数据类型 , 后面带 Wrapper 是包装类 Complex Type 是复杂类型
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 到复杂类型 , 如果复杂类型包含一个 String 类型的构造器
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.
在普通Java项目中使用Dozer
在pom.xml中增加依赖
&dependency&
&groupId&net.sf.dozer&/groupId&
&artifactId&dozer&/artifactId&
&version&5.5.1&/version&
&/dependency&
使用Dozer进行类转换
public class Main {
public static void main(String[] args) {
DozerBeanMapper mapper = new DozerBeanMapper();
UserDO userDO = new UserDO();
userDO.setName(&hollis&);
userDO.setAddress(&hz&);
userDO.setAge(25);
userDO.setCompanyId(1);
userDO.setBirthday(new Date());
userDO.setGender(&male&);
userDO.setEducation(&1&);
userDO.setNickName(&hollis&);
userDO.setPoliticalStatus(&3&);
UserVO userVO = (UserVO) mapper.map(userDO, UserVO.class);
System.out.println(userVO);
特殊的字段转换
在使用mapper进行转换前,设置一个或多个mapping文件
List myMappingFiles = new ArrayList();
myMappingFiles.add(&dozer-mapping.xml&);
mapper.setMappingFiles(myMappingFiles);
配置dozer-mapping.xml文件
数据类型不一致,或者名称不相同或者有级联关系等情况下,可以通过文件dozer-mapping.xml来进行定制Bean的转换
&?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&&
&class-a&com.hollis.lab.UserDO&/class-a&
&class-b&com.hollis.lab.UserVO&/class-b&
&/mapping&
&/mappings&
在JavaWeb项目中使用Dozer
在pom.xml中增加依赖
&dependency&
&groupId&net.sf.dozer&/groupId&
&artifactId&dozer&/artifactId&
&version&5.5.1&/version&
&/dependency&
使用Spring集成dozer
&?xml version=&1.0& encoding=&UTF-8&?&
&!DOCTYPE beans PUBLIC &-//SPRING//DTD BEAN//EN& &http://www.springframework.org/dtd/spring-beans.dtd&&
&bean id=&baseMapper& class=&org.dozer.spring.DozerBeanMapperFactoryBean&&
&property name=&mappingFiles&&
&value&classpath:mapping/dozer-mapping.xml&/value&
&/property&
使用baseMapper进行Bean的转换
@Autowired
private Mapper baseM
private UserVO doToVo(UserDO userDO){
if(userDO == null)
UserVO vo = baseMapper.map(userDO, UserVO.getClass());
if(userDO.getCompanyId != null) getCompany(vo);
通过以上的代码加配置,我们就实现了从DO转换到VO的部分操作,之所以说是部分操作,是因为我们在dozer-mapping.xml并没有做多余的配置,只是使用dozer将DO中和VO中共有的属性转换了过来。对于其他的类型不同或者名称不同等的转换可以参考官网例子通过设置dozer-mapping.xml文件来实现。
上面还有一个getCompany()没有实现。这个方法其实就是通过companyId查询出company实体然后在赋值给UserVO中的company属性。
在使用了dozer之后,我们可以把UserDO中的部分属性赋值到UserVO中,其实,转化的过程是通过调用UserDO中的getter方法和UserVO中的setter方法来实现的。读者可以做个实验,对于UserVO中的部分属性不写Setter方法看看还能不能把属性值转换过来,博主已经测试过了,是不能的。
可能感兴趣的文章
多了解一下挺好
关于ImportNew
ImportNew 专注于 Java 技术分享。于日 11:11正式上线。是的,这是一个很特别的时刻 :)
ImportNew 由两个 Java 关键字 import 和 new 组成,意指:Java 开发者学习新知识的网站。 import 可认为是学习和吸收, new 则可认为是新知识、新技术圈子和新朋友……
新浪微博:
推荐微信号
反馈建议:@
广告与商务合作QQ:
– 好的话题、有启发的回复、值得信赖的圈子
– 写了文章?看干货?去头条!
– 为IT单身男女服务的征婚传播平台
– 优秀的工具资源导航
– 活跃 & 专业的翻译小组
– 国内外的精选博客文章
– UI,网页,交互和用户体验
– JavaScript, HTML5, CSS
– 专注Android技术分享
– 专注iOS技术分享
– 专注Java技术分享
– 专注Python技术分享
& 2017 ImportNew& 1&package&org.springside.examples.showcase.utilities.&&2&&&3&import&static&org.junit.Assert.*;&&4&&&5&import&java.util.L&&6&&&7&import&org.dozer.M&&8&import&org.junit.T&&9&import&org.springside.modules.mapper.BeanM&10&&11&/**&12&&*&演示Dozer如何只要属性名相同,可以罔顾属性类型是基础类型&-&String的差别,Array转为List,甚至完全另一种DTO的差别。&13&&*&并且能完美解决循环依赖问题。&14&&*&使用@Mapping能解决属性名不匹配的情况.&15&&*/&16&public&class&DozerDemo&{&17&&18&&&&&/**&19&&&&&&*&从一个ProductDTO实例,创建出一个新的Product实例。&20&&&&&&*/&21&&&&&@Test&22&&&&&public&void&map()&{&23&&&&&&&&&ProductDTO&productDTO&=&new&ProductDTO();&24&&&&&&&&&productDTO.setName("car");&25&&&&&&&&&productDTO.setPrice("200");&26&&27&&&&&&&&&PartDTO&partDTO&=&new&PartDTO();&28&&&&&&&&&partDTO.setName("door");&29&&&&&&&&&partDTO.setProduct(productDTO);&30&&31&&&&&&&&&productDTO.setParts(new&PartDTO[]&{&partDTO&});&32&&33&&&&&&&&&//ProductDTO-&Product&34&&&&&&&&&Product&product&=&BeanMapper.map(productDTO,&Product.class);&35&&36&&&&&&&&&assertEquals("car",&product.getProductName());&37&&&&&&&&&//原来的字符串被Map成Double。&38&&&&&&&&&assertEquals(new&Double(200),&product.getPrice());&39&&&&&&&&&//原来的PartDTO同样被Map成Part&,Array被Map成List&40&&&&&&&&&assertEquals("door",&product.getParts().get(0).getName());&41&&&&&&&&&//Part中循环依赖的Product同样被赋值。&42&&&&&&&&&assertEquals("car",&product.getParts().get(0).getProduct().getProductName());&43&&44&&&&&&&&&//再反向从Product-&ProductDTO&45&&&&&&&&&ProductDTO&productDTO2&=&BeanMapper.map(product,&ProductDTO.class);&46&&&&&&&&&assertEquals("car",&productDTO2.getName());&47&&&&&&&&&assertEquals("200.0",&productDTO2.getPrice());&48&&&&&&&&&assertEquals("door",&productDTO2.getParts()[0].getName());&49&&&&&}&50&&51&&&&&/**&52&&&&&&*&演示将一个ProductDTO实例的内容,Copy到另一个已存在的Product实例.&53&&&&&&*/&54&&&&&@Test&55&&&&&public&void&copy()&{&56&&&&&&&&&ProductDTO&productDTO&=&new&ProductDTO();&57&&&&&&&&&productDTO.setName("car");&58&&&&&&&&&productDTO.setPrice("200");&59&&60&&&&&&&&&PartDTO&partDTO&=&new&PartDTO();&61&&&&&&&&&partDTO.setName("door");&62&&&&&&&&&partDTO.setProduct(productDTO);&63&&64&&&&&&&&&productDTO.setParts(new&PartDTO[]&{&partDTO&});&65&&66&&&&&&&&&//已存在的Product实例&67&&&&&&&&&Product&product&=&new&Product();&68&&&&&&&&&product.setProductName("horse");&69&&&&&&&&&product.setWeight(new&Double(20));&70&&71&&&&&&&&&BeanMapper.copy(productDTO,&product);&72&&73&&&&&&&&&//原来的horse,被替换成car&74&&&&&&&&&assertEquals("car",&product.getProductName());&75&&&&&&&&&//原来的20的属性被覆盖成200,同样被从字符串被专为Double。&76&&&&&&&&&assertEquals(new&Double(200),&product.getPrice());&77&&&&&&&&&//DTO中没有的属性值,在Product中被保留&78&&&&&&&&&assertEquals(new&Double(20),&product.getWeight());&79&&&&&&&&&//Part中循环依赖的Product同样被赋值。&80&&&&&&&&&assertEquals("car",&product.getParts().get(0).getProduct().getProductName());&81&&&&&}&82&&83&&&&&public&static&class&Product&{&84&&&&&&&&&private&String&productN&85&&&&&&&&&private&Double&&86&&&&&&&&&private&List&Part&&&87&&&&&&&&&//DTO中没有的属性&88&&&&&&&&&private&Double&&89&&90&&&&&&&&&public&String&getProductName()&{&91&&&&&&&&&&&&&return&productN&92&&&&&&&&&}&93&&94&&&&&&&&&public&void&setProductName(String&productName)&{&95&&&&&&&&&&&&&this.productName&=&productN&96&&&&&&&&&}&97&&98&&&&&&&&&public&Double&getPrice()&{&99&&&&&&&&&&&&&return&100&&&&&&&&&}101&102&&&&&&&&&public&void&setPrice(Double&price)&{103&&&&&&&&&&&&&this.price&=&104&&&&&&&&&}105&106&&&&&&&&&public&List&Part&&getParts()&{107&&&&&&&&&&&&&return&108&&&&&&&&&}109&110&&&&&&&&&public&void&setParts(List&Part&&parts)&{111&&&&&&&&&&&&&this.parts&=&112&&&&&&&&&}113&114&&&&&&&&&public&Double&getWeight()&{115&&&&&&&&&&&&&return&116&&&&&&&&&}117&118&&&&&&&&&public&void&setWeight(Double&weight)&{119&&&&&&&&&&&&&this.weight&=&120&&&&&&&&&}121&122&&&&&}123&124&&&&&public&static&class&Part&{125&&&&&&&&&//反向依赖Product126&&&&&&&&&private&Product&127&128&&&&&&&&&private&String&129&130&&&&&&&&&public&String&getName()&{131&&&&&&&&&&&&&return&132&&&&&&&&&}133&134&&&&&&&&&public&void&setName(String&name)&{135&&&&&&&&&&&&&this.name&=&136&&&&&&&&&}137&138&&&&&&&&&public&Product&getProduct()&{139&&&&&&&&&&&&&return&140&&&&&&&&&}141&142&&&&&&&&&public&void&setProduct(Product&product)&{143&&&&&&&&&&&&&this.product&=&144&&&&&&&&&}145&&&&&}146&147&&&&&public&static&class&ProductDTO&{148&&&&&&&&&//定义到Product中的productName,只要在一边定义,双向转换都可以使用.149&&&&&&&&&@Mapping("productName")150&&&&&&&&&private&String&151&&&&&&&&&//类型为String&而非&Double152&&&&&&&&&private&String&153&&&&&&&&&//类型为Array而非List,&PartDTO而非Part154&&&&&&&&&private&PartDTO[]&155&156&&&&&&&&&public&String&getName()&{157&&&&&&&&&&&&&return&158&&&&&&&&&}159&160&&&&&&&&&public&void&setName(String&name)&{161&&&&&&&&&&&&&this.name&=&162&&&&&&&&&}163&164&&&&&&&&&public&String&getPrice()&{165&&&&&&&&&&&&&return&166&&&&&&&&&}167&168&&&&&&&&&public&void&setPrice(String&price)&{169&&&&&&&&&&&&&this.price&=&170&&&&&&&&&}171&172&&&&&&&&&public&PartDTO[]&getParts()&{173&&&&&&&&&&&&&return&174&&&&&&&&&}175&176&&&&&&&&&public&void&setParts(PartDTO[]&parts)&{177&&&&&&&&&&&&&this.parts&=&178&&&&&&&&&}179&&&&&}180&181&&&&&public&static&class&PartDTO&{182&&&&&&&&&//反向依赖ProductDTO183&&&&&&&&&private&ProductDTO&184&185&&&&&&&&&private&String&186&187&&&&&&&&&public&String&getName()&{188&&&&&&&&&&&&&return&189&&&&&&&&&}190&191&&&&&&&&&public&void&setName(String&name)&{192&&&&&&&&&&&&&this.name&=&193&&&&&&&&&}194&195&&&&&&&&&public&ProductDTO&getProduct()&{196&&&&&&&&&&&&&return&197&&&&&&&&&}198&199&&&&&&&&&public&void&setProduct(ProductDTO&product)&{200&&&&&&&&&&&&&this.product&=&201&&&&&&&&&}202&203&&&&&}204&205&}206&&
2627282930311234568912141516182021222324252627282930123456
<a style="border-bottom-color: #ff0000; border-top-color: #ff0000; color: # border-right-color: #ff0000; border-left-color: #ff0000" href="/" alt=""
我好喜欢它,
我想做技术流,可是我很懒,
博客新地址:&a href="" &
随笔分类(19)
文章档案(1)
阅读排行榜java - How to use Dozer with Spring Boot? - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 7.0 million programmers, just like you, helping each other.
J it only takes a minute:
I am working on a Spring Boot project. I just have annotation configuration. I want to include dozer to transform Entities to DTO and DTO to Entities. I see in the dozer website, they explain i have to add the following configuration in spring xml configuration file. Since i have not xml file but annotation configuration Java class, i don't know how to translate this into Java Configuration class.
&bean id="org.dozer.Mapper" class="org.dozer.DozerBeanMapper"&
&property name="mappingFiles"&
&value&dozer-global-configuration.xml&/value&
&value&dozer-bean-mappings.xml&/value&
&value&more-dozer-bean-mappings.xml&/value&
&/property&
If someone could you give me an example it'll be very useful. Thanks
1,72753171
I think something like this should work:
@Configuration
public class YourConfiguration {
@Bean(name = "org.dozer.Mapper")
public DozerBeanMapper dozerBean() {
List&String& mappingFiles = Arrays.asList(
"dozer-global-configuration.xml",
"dozer-bean-mappings.xml",
"more-dozer-bean-mappings.xml"
DozerBeanMapper dozerBean = new DozerBeanMapper();
dozerBean.setMappingFiles(mappingFiles);
return dozerB
21.9k84463
Did you find this question interesting? Try our newsletter
Sign up for our newsletter and get our top new questions delivered to your inbox ().
Subscribed!
Success! Please click the link in the confirmation email to activate your subscription.
If you are using DozerBeanMapperFactoryBean instead of DozerBeanMapper you may use something like this.
@Configuration
public class MappingConfiguration {
public DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean(@Value("classpath*:mappings/*mappings.xml") Resource[] resources) throws Exception {
final DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean = new DozerBeanMapperFactoryBean();
// Other configurations
dozerBeanMapperFactoryBean.setMappingFiles(resources);
return dozerBeanMapperFactoryB
This way you can import your mappings automatically. Than simple inject your Mapper and use.
@Autowired
2,72511936
Just in case someone wants to avoid xml dozer file. You can use a builder directly in java. For me it's the way to go in a annotation Spring context.
See more information at
public DozerBeanMapper mapper() throws Exception {
DozerBeanMapper mapper = new DozerBeanMapper();
mapper.addMapping(objectMappingBuilder);
BeanMappingBuilder objectMappingBuilder = new BeanMappingBuilder() {
protected void configure() {
mapping(Bean1.class, Bean2.class)
.fields("id", "id").fields("name", "name");
In my case it was more efficient (At least the first time). Didn't do any benchmark or anything.
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you&#39;re looking for?
Browse other questions tagged
The week&#39;s top questions and answers
Important community announcements
Questions that need answers
rev .25649
Stack Overflow works best with JavaScript enabledDozer 使用 - 刘小龙 - ITeye技术网站
博客分类:
Dozer用于 model 之间转换
Maven 引用dozer 5.4.0
&dependency&
&groupId&net.sf.dozer&/groupId&
&artifactId&dozer&/artifactId&
&version&5.4.0&/version&
&/dependency&
public class Book {
public Book() {
public void setAuthor(String author) {
this.author =
public String getAuthor() {
return (this.author);
public void setName(String name) {
this.name =
public String getTest() {
public void setTest(String test) {
this.test =
public String getName() {
return this.
2. BookDTO
public class BookDTO {
private String bookN
public String getBookName() {
return bookN
public void setBookName(String bookName) {
this.bookName = bookN
public String getAuthor() {
public void setAuthor(String author) {
this.author =
3. dozerBeanMapping.xml 配置对象的映射
&?xml version="1.0" encoding="UTF-8"?&
&!DOCTYPE mappings PUBLIC "-//DOZER//DTD MAPPINGS//EN"
"http://dozer.sourceforge.net/dtd/dozerbeanmapping.dtd"&
&mappings&
&configuration&
&stop-on-errors&false&/stop-on-errors&
&date-format&MM/dd/yyyy HH:mm&/date-format&
&wildcard&true&/wildcard&
&/configuration&
&class-a&entity.Book&/class-a&
&class-b&entity.BookDTO&/class-b&
&a&name&/a&
&b&bookName&/b&
&a&author&/a&
&b&author&/b&
&/mapping&
&/mappings&
package dozer.
import java.util.ArrayL
import java.util.L
import org.dozer.DozerBeanM
import org.dozer.DozerBeanMapperSingletonW
import org.dozer.M
import entity.B
import entity.BookDTO;
public class DozerDemoTest {
public static void main(String[] args) {
Book book1 = new Book();
book1.setAuthor("dennis");
book1.setName("dozer demo");
book1.setTest("test fields");
DozerBeanMapper mapper = new DozerBeanMapper();
Book book2 = new Book();
mapper.map(book1, book2);
book2 = (Book) mapper.map(book1, Book.class);
System.out.println("book2's name:" + book2.getName());
DozerBeanMapper mapper2 = new DozerBeanMapper();
List myMappingFiles = new ArrayList();
myMappingFiles.add("dozerBeansMapping.xml");
mapper2.setMappingFiles(myMappingFiles);
BookDTO bookDto = mapper2.map(book1, BookDTO.class);
System.out.println(bookDto.toString());
Mapper mapper3 = DozerBeanMapperSingletonWrapper.getInstance();
BookDTO bookDto3 = mapper3.map(book1, BookDTO.class);
二. Dozer 用spring管理
项目引用Spring jar包
新建spring 配置文件 spring-dozer.xml
&?xml version="1.0" encoding="UTF-8"?&
&beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"&
&context:annotation-config /&
&bean id="mapper" class="org.dozer.spring.DozerBeanMapperFactoryBean"&
&property name="mappingFiles"&
&!-- &value&dozer-global-configuration.xml&/value& --&
&value&dozer-bean-mappings.xml&/value&
&!-- &value&more-dozer-bean-mappings.xml&/value& --&
&/property&
新建dozer映射配置文件 dozer-bean-mappings.xml
&?xml version="1.0" encoding="UTF-8"?&
&!DOCTYPE mappings PUBLIC "-//DOZER//DTD MAPPINGS//EN"
"http://dozer.sourceforge.net/dtd/dozerbeanmapping.dtd"&
&mappings&
&configuration&
&stop-on-errors&false&/stop-on-errors&
&date-format&MM/dd/yyyy HH:mm&/date-format&
&wildcard&true&/wildcard&
&/configuration&
&class-a&entity.Book&/class-a&
&class-b&entity.BookDTO&/class-b&
&a&name&/a&
&b&bookName&/b&
&a&author&/a&
&b&author&/b&
&/mapping&
&/mappings&
新建测试类
package dozer.
import org.dozer.M
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
import entity.B
import entity.BookDTO;
public class DozerWithSpringTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dozer.xml");
Book book1 = new Book();
book1.setAuthor("dennis");
book1.setName("dozer demo");
book1.setTest("test fields");
Mapper mapper = (Mapper) ctx.getBean("mapper");
BookDTO bookDto =
mapper.map(book1, BookDTO.class);
三、 不用spring, 但是dozer mapper要单例, dozer 映射文件可以不用dozerBeanMapping.xml
/**&
* dozer单例的wrapper.
* dozer在同一jvm里使用单例即可,无需重复创建.
* 但Dozer4.0自带的DozerBeanMapperSingletonWrapper必须使用dozerBeanMapping.xml作参数初始化,因此重新实现.
* 实现PO到VO的深层次复制
public class DozerMapperSingleton {
private static M
private DozerMapperSingleton() {
//shoudn't invoke.
public static synchronized Mapper getInstance() {
if (instance == null) {
instance = new DozerBeanMapper();
用的时候直接获取实例就OK了,&
AgentContractInfo info=new AgentContractInfo ();//假设这个对象是有值的&
AgentContract acr = new AgentContract();&
DozerMapperSingleton.getInstance().map(info, acr);&&
//这样 info对象中的所有属性复制到info中去了
四、Only Using one mapping file:
A file called dozerBeanMapping.xml file will be loaded if it is in your Classpath. You can find a sample of this file in the {dozer.home}/mappings directory.
这样就不用spring去管理了
Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
DestinationObject destObject =
mapper.map(sourceObject, DestinationObject.class);
Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
DestinationObject destObject = new DestinationObject();
mapper.map(sourceObject, destObject);
下载次数: 30
(561.5 KB)
下载次数: 40
下载次数: 29
浏览: 5701 次
来自: 南昌}

我要回帖

更多关于 dozer spring集成 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信