Dozer使用总结

Dozer使用总结

 

     概念

1.1 什么是dozer?

Dozer 是一个对象转换工具。

Dozer可以在JavaBean到JavaBean之间进行递归数据复制,并且这些JavaBean可以是不同的复杂的类型。

所有的mapping,Dozer将会很直接的将名称相同的fields进行复制,如果field名不同,或者有特别的对应要求,则可以在xml中进行定义。

更多详细请参考dozer官网:http://dozer.sourceforge.net/documentation/about.html

1.2 为什么要使用Dozer?

分析多层架构的J2EE系统,经常存在JavaBean直接的拷贝。比如我们在DAO层,通过Do取得业务层需要的数据,将这些数据传递给Service层的VO。Do与VO就存在典型的值拷贝。

典型的解决方案就是手动拷贝,弊端很明显,代码中充斥大量Set 和Get方法,真正的业务被埋藏值与值的拷贝之中。另一种方案就是使用BeanUtil,但BeanUtil不够很好的灵活性,又时候还不得不手动拷贝。Dozer可以灵活的对对象进行转换,且使用简单。

 

注意:Dozer支持简单类型复杂类型的双向递归映射。

1.3 如何得到Dozer工具类jar包

1.点击下载:http://sourceforge.net/projects/dozer/files/

 

2.如果使用maven工具的话,在pom.Xml文件中加入:

  <dependency>

      <groupId>net.sf.dozer</groupId>

      <artifactId>dozer</artifactId>

      <version>5.2.1</version>

 
    </ dependency >

加入你的构建路径既可以使用。

     Dozer 支持的转换类型

Dozer支持

Primitive 基本数据类型, 后面带 Wrapper 是包装类 Complex Type 是复杂类型

  • Primitive to PrimitiveWrapper
  • Primitive to CustomWrapper
  • Primitive Wrapper to PrimitiveWrapper
  • Primitive to Primitive
  • Complex Type to ComplexType
  • String to Primitive
  • String to PrimitiveWrapper
  • String to Complex Type if the ComplexType 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 oneanother: java.util.Date, java.sql.Date, java.sql.Time,java.sql.Timestamp, java.util.Calendar,java.util.GregorianCalendar
  • String to any of the supportedDate/Calendar Objects.
  • Objects containing a toString()method that produces a long representing time in (ms) to anysupported Date/Calendar object.

     Dozer使用(不与spring集成下)

3.1 dozer使用分类

根据有无映射文件和文件的多少,有三种方式:


第一种:该方式用于数据类型为基本类型,名称相同的对象映射

Mapper mapper =new DozerBeanMapper();

SourceObject sourceObject =new SourceObject();

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

   //  or

DestinationObject destObject =new DestinationObject();

mapper.map(sourceObject,destObject);

第二种:该方式用于数据类型不一致,或者名称不相同或者有级联关系等情况下的映射,该方式可以添加多个配置文件dozerBeanMapping.xmlsomeOtherDozerBeanMappings.xml 

List myMappingFiles =new ArrayList();

myMappingFiles.add("dozerBeanMapping.xml");

//myMappingFiles.add("someOtherDozerBeanMappings.xml");

DozerBeanMapper mapper =new DozerBeanMapper();

SourceObject sourceObject =new SourceObject();

mapper.setMappingFiles(myMappingFiles);

DestinationObjectstObject=

(DestinationObject)mapper.map(sourceObject, DestinationObject.class);

第三种:该方式用于数据类型不一致,或者名称不相同或者有级联关系等情况下的映射,配置文件只有一个映射文件叫dozerBeanMapping.xml且在根目录下

Mapper mapper =DozerBeanMapperSingletonWrapper.getInstance();

SourceObject sourceObject =new SourceObject();

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

//or

//Mapper mapper =DozerBeanMapperSingletonWrapper.getInstance();

//DestinationObject destObject = newDestinationObject();

mapper.map(sourceObject,destObject);

 

3.2 举例说明:

假设我们现在有一个userDo类,如下:


package ce.dozer;

public class User {

   private int id;

   private String name;

   private String password;

   private Info info;

   public int getId() {

      return id;

   }

   public void setId(int id) {

      this.id = id;

   }

   public String getName() {

      return name;

   }

   public void setName(String name) {

      this.name = name;

   }

   public String getPassword() {

      return password;

   }

   public void setPassword(String password){

      this.password = password;

   }

   public Info getInfo() {

      return info;

   }

   public void setInfo(Info info) {

      this.info = info;

   }

}

一个userVo类,如下:


package ce.dozer;

public class UserVO {

   private int id;

   private String userName;

   private String password;

   private InfoVO info;

   public int getId() {

      return id;

   }

   public void setId(int id) {

      this.id = id;

   }

   public String getUserName() {

      return userName;

   }

   public void setUserName(String userName){

      this.userName = userName;

   }

   public String getPassword() {

      return password;

   }

   public void setPassword(String password){

      this.password = password;

   }

   public InfoVO getInfo() {

      return info;

   }

   public void setInfo(InfoVO info) {

      this.info = info;

   }

}

一个性別枚舉类,如下:

  package ce.dozer;

public enum GenderType {

   male,//

   female//

}

一个infoDo类,如下:


package ce.dozer;

import java.util.Date;

public class Info {

   private int id;

   private Date createDate;

   private GenderType gender;

   public int getId() {

      return id;

   }

   public void setId(int id) {

      this.id = id;

   }
~~~~~~setget~~~~~~~

一个infoVo类,如下:


package ce.dozer;

public class InfoVO {

   private int id;

   private String date;

   private Integer gender;

   public int getId() {

      return id;

   }

在给出的示例中我们可以看到userDouserVo中的字段对应关系

userVouserDo不同
id(int)id(int) 
userName(String)name(String)名称不同
password(String)password(String) 
Info(InfoVo)Info(Info)类型不同

再看看infoDoinfoVo 中的字段对应关系

综合上面的问题我们可以如下

infoVoinfoDo不同
id(int)id(int) 
createDate(String)date(Date)类型不同
gender(Integer)gender(GenderType)枚举类型不同

 

解决方式:

 

3.2.1       字段名称不同映射配置


<mapping>

      <class-a>ce.dozer.User</class-a>

      <class-b>ce.dozer.UserVO</class-b>

      <field>

          <a>name</a>

          <b>userName</b>

      </field>

          </mapping>

3.2.2       Date与String映射配置如下:


<mapping date-format="yyyy-MM-dd">

      <class-a>ce.dozer.User</class-a>

      <class-b>ce.dozer.UserVO</class-b>

      <field>

          <a>info.createDate</a>

          <b>info.date</b>

      </field>

</mapping>

3.2.3       在示例中我们看到在userDo和userVo对象中关联了其他的对象,这个问题不用担心,因为对象名称相同dozer会为我们自动转换。而问题不在这,在与对象中有枚举类型,我们该怎么写配置呢?在这里我们就必须自己编写一个处理类来处理枚举与Integer的转换了(假设我们的处理类为util.EnumIntConverter),配置如下:

< configuration >

   <stop-on-errors>true</stop-on-errors>

   <date-format>yyyy-MM-dd</date-format>

   <wildcard>true</wildcard>

   <custom-converters>

      <converter type=" util.EnumIntConverter">

         <class-a>java.lang.Enum</class-a>

         <class-b>java.lang.Integer</class-b>

      </converter>

   </custom-converters>

</configuration>

<mapping>

   <class-a>ce.dozer.User</class-a>

   <class-b>ce.dozer.UserVO</class-b>

   <field>

      <a>info.gender</a>

      <b>info.gender</b>

   </field>

</mapping>  

最终的dozerBeanMapping.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">

   <configuration>

      <stop-on-errors>true</stop-on-errors>

      <date-format>yyyy-MM-dd</date-format>

      <wildcard>true</wildcard>

      <custom-converters>

          <converter type="util.EnumIntConverter">

             <class-a>java.lang.Enum</class-a>

             <class-b>java.lang.Integer</class-b>

          </converter>

      </custom-converters>

   </configuration>

   <mapping date-format="yyyy-MM-dd">

      <class-a>ce.dozer.User</class-a>

      <class-b>ce.dozer.UserVO</class-b>

      <field>

          <a>info.createDate</a>

          <b>info.date</b>

      </field>

      <field>

          <a>info.gender</a>

          <b>info.gender</b>

      </field>

      <field>

          <a>info.createDate</a>

          <b>info.date</b>

      </field>

      <field>

          <a>name</a>

          <b>userName</b>

      </field>

   </mapping>

</ mappings >

     与spring的集成

4.1 1、dozer 要与spring集成需要将dozer交给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:tx="http://www.springframework.org/schema/tx"

   xmlns:aop="http://www.springframework.org/schema/aop"

   xsi:schemaLocation="

   http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd

   http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd

   http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd"

   default-autowire="byName" default-lazy-init="false">

 

   <bean id="mapper" class="org.dozer.spring.DozerBeanMapperFactoryBean">

      <property name="mappingFiles">

<list>       <value>classpath*:dozer-config/dozerBeanMapping.xml</value>

          </list>

      </property>

   </bean>

</ beans >

4.2 2、当我们的dozer交给了spring管理,当spring容器启动后我们可以将下面的代码添加到用于继承的基类中去:


private Mappermapper ;

public Mapper getMapper(){

   return mapper;

}

public void setMapper(Mapper mapper){

   this.mapper = mapper;

}

4.3 3、在继承者类中就可以直接如下使用:

getMapper().map( sourceObject,DestinationObject. class)

     结束语

Dozer的转换功能很强大,我们所能想到的类型转换,它基本都可以帮助我们完成。所以如果您想对dozer了解更多更深,建议到官网仔细阅读相关文档和说明!

官网:http://dozer.sourceforge.net/documentation/mappings.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值