Dozer使用总结

Dozer使用总结

 

1       概念

1.1  什么是dozer

Dozer 是一个对象转换工具。

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

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

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

1.2  为什么要使用Dozer

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

典型的解决方案就是手动拷贝,弊端很明显,代码中充斥大量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.dozergroupId>

       <artifactId>dozerartifactId>

       <version>5.2.1version>

      dependency>

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

2       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.

3       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);

DestinationObject stObject=

(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 = new DestinationObject();

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;

    }
~~~~~~set\get~~~~~~~

一个infoVo类,如下:

 

package ce.dozer;

public class InfoVO {

    private int id;

    private String date;

    private Integer gender;

    public int getId() {

       return id;

    }

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

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

再看看infoDo infoVo 中的字段对应关系

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

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

 

解决方式:

 

3.2.1        字段名称不同映射配置

 

<mapping>

       <class-a>ce.dozer.Userclass-a>

       <class-b>ce.dozer.UserVOclass-b>

       <field>

           <a>namea>

           <b>userNameb>

       

      

3.2.2        DateString映射配置如下:

 

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

       <class-a>ce.dozer.Userclass-a>

       <class-b>ce.dozer.UserVOclass-b>

       <field>

           <a>info.createDatea>

           <b>info.dateb>

       

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

<configuration>

    <stop-on-errors>truestop-on-errors>

    <date-format>yyyy-MM-dddate-format>

    <wildcard>truewildcard>

    

       util.EnumIntConverter">

           java.lang.Enum

           java.lang.Integer

       

    

configuration>

<mapping>

    <class-a>ce.dozer.Userclass-a>

    <class-b>ce.dozer.UserVOclass-b>

    <field>

        <a>info.gendera>

        <b>info.genderb>

    

 

最终的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>truestop-on-errors>

       <date-format>yyyy-MM-dddate-format>

       <wildcard>truewildcard>

       <custom-converters>

           <converter type=" util.EnumIntConverter">

              <class-a>java.lang.Enumclass-a>

              <class-b>java.lang.Integerclass-b>

           converter>

       custom-converters>

    configuration>

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

       <class-a>ce.dozer.Userclass-a>

       <class-b>ce.dozer.UserVOclass-b>

       <field>

           <a>info.createDatea>

           <b>info.dateb>

       field>

        <field>

           <a>info.gendera>

           <b>info.genderb>

       field>

       <field>

           <a>info.createDatea>

           <b>info.dateb>

       field>

       <field>

           <a>namea>

           <b>userNameb>

       field>

    mapping>

mappings>

4       spring的集成

4.1  1dozer 要与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/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

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

    http://www.springframework.org/schema/aop http://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.xmlvalue>

           list>

       property>

    bean>

beans>

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

 

private Mapper mapper ;

public Mapper getMapper() {

    return mapper;

}

public void setMapper(Mapper mapper) {

    this.mapper = mapper;

}

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

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

5       结束语

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

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

你可能感兴趣的:(java编程,spring,xml,maven)