Dozer 使用

Dozer用于 model 之间转换
Maven 引用dozer 5.4.0

<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.4.0</version>
</dependency>

1. Book

package entity;

public class Book {

private String name;
private String author;
private String test;

public Book() {

}

public void setAuthor(String author) {
this.author = author;
}

public String getAuthor() {
return (this.author);
}

public void setName(String name) {
this.name = name;
}

public String getTest() {
return test;
}

public void setTest(String test) {
this.test = test;
}

public String getName() {
return this.name;
}
}

2. BookDTO

package entity;

public class BookDTO {
private String bookName;
private String author;
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = 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>
<mapping>
<class-a>entity.Book</class-a>
<class-b>entity.BookDTO</class-b>
<field>
<a>name</a>
<b>bookName</b>
</field>
<field>
<a>author</a>
<b>author</b>
</field>
</mapping>
</mappings>

4. 测试类

package dozer.test;

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;
import org.dozer.DozerBeanMapperSingletonWrapper;
import org.dozer.Mapper;

import entity.Book;
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">
<list>
<!-- <value>dozer-global-configuration.xml</value> -->
<value>dozer-bean-mappings.xml</value>
<!-- <value>more-dozer-bean-mappings.xml</value> -->
</list>
</property>
</bean>
</beans>

新建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>
<mapping>
<class-a>entity.Book</class-a>
<class-b>entity.BookDTO</class-b>
<field>
<a>name</a>
<b>bookName</b>
</field>
<field>
<a>author</a>
<b>author</b>
</field>
</mapping>
</mappings>

新建测试类

package dozer.test;

import org.dozer.Mapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import entity.Book;
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 Mapper instance;

private DozerMapperSingleton() {
//shoudn't invoke.
}

public static synchronized Mapper getInstance() {
if (instance == null) {
instance = new DozerBeanMapper();
}
return instance;
}

}

用的时候直接获取实例就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);

or

Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
DestinationObject destObject = new DestinationObject();
mapper.map(sourceObject, destObject);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值