准确的说,是因为pojo无法一招走天下或者说内外部隔离的原因,所以有些时候,不得不在两个bean之间做copy或者转换映射。对于直接性的属性拷贝beanutil以及能够满足大部分要求,但是如果遇到字段不一致或者需要二次处理的情况下,就需要进行人工代码处理了。而且这些重复除非通过某种方式管理起来,不然系统中会有大量的复制粘贴。

周六的时候,一个同事说他们那边使用的dozer,还挺好用的,于是看了下官方手册http://dozer.sourceforge.net/dozer-user-guide.pdf,确实比较灵活,有点类似于mybatis之余jdbc原生代码。DEMO示例(实际使用中建议使用spring IOC方式,以及classpath加载,故非以教程式,而是直接可用的代码为例子)如下。

原bean:

package test;

public class SourceObject {
private String srcName;
private int age;
private String notMatch;
public String getSrcName() {
return srcName;
}
public void setSrcName(String srcName) {
this.srcName = srcName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getNotMatch() {
return notMatch;
}
public void setNotMatch(String notMatch) {
this.notMatch = notMatch;
}
}

目标bean:

package test;

public class DestinationObject {
private String destName;
private int age;
private String notExist;
private String dictName;
public String getDestName() {
return destName;
}
public void setDestName(String destName) {
this.destName = destName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getNotExist() {
return notExist;
}
public void setNotExist(String notExist) {
this.notExist = notExist;
}
public String getDictName() {
return dictName;
}
public void setDictName(String dictName) {
this.dictName = dictName;
} public void dictSet(String destName) {
this.dictName = "dict " + destName;
}
}

转换映射文件:

<?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>MM/dd/yyyy HH:mm</date-format> <!-- 设置java.util.Date的默认格式, 用于 -->
<wildcard>true</wildcard>
</configuration> <mapping>
<class-a>test.SourceObject</class-a>
<class-b>test.DestinationObject</class-b>
<field>
<a>srcName</a>
<b>destName</b>
</field>
<field>
<a>srcName</a> <!-- 一个属性可以映射到多个目标 -->
<b set-method="dictSet">dictName</b> <!-- 会将destName作为参数传递给dictSet方法 -->
</field>
</mapping>
<!-- other custom class mappings would go here....... --> </mappings>

spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<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-4.0.xsd"
default-autowire="byName"
default-lazy-init="false"> <bean id="mapper" class="org.dozer.spring.DozerBeanMapperFactoryBean">
<property name="mappingFiles">
<list>
<value>classpath*:*DTOMapper.xml</value>
</list>
</property>
</bean>
</beans>

测试类:

package test;

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
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 org.springframework.test.context.transaction.TransactionConfiguration; import com.alibaba.fastjson.JSON; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-dozer.xml"})
@TransactionConfiguration(defaultRollback = false)
public class DozerBeanMapperTest {
@Autowired
Mapper mapperNotDefault; @Test
public void testDozerBeanMapper(){
SourceObject sourceObject = new SourceObject();
sourceObject.setAge(1);
sourceObject.setNotMatch("notmatch");
sourceObject.setSrcName("name1");
DestinationObject destObject;
Mapper mapper = new DozerBeanMapper();
destObject = mapper.map(sourceObject, DestinationObject.class);
System.out.println(JSON.toJSONString(sourceObject));
System.out.println(JSON.toJSONString(destObject)); destObject = mapperNotDefault.map(sourceObject, DestinationObject.class);
System.out.println(JSON.toJSONString(sourceObject));
System.out.println(JSON.toJSONString(destObject));
}
}

输出如下:

{"age":1,"notMatch":"notmatch","srcName":"name1"}
{"age":1}
{"age":1,"notMatch":"notmatch","srcName":"name1"}
{"age":1,"destName":"name1","dictName":"dict name1"}

比beanutil更加灵活的dto转换工具dozer的更多相关文章

  1. .NET的DTO映射工具AutoMapper

    .NET的DTO映射工具AutoMapper 原文:https://github.com/AutoMapper/AutoMapper/wiki/Getting-started 参考:http://ww ...

  2. 对象转换工具 MapStruct 介绍

    前言 在我们日常开发的分层结构的应用程序中,为了各层之间互相解耦,一般都会定义不同的对象用来在不同层之间传递数据,因此,就有了各种 XXXDTO.XXXVO.XXXBO 等基于数据库对象派生出来的对象 ...

  3. Unicode编码解码在线转换工具

    // Unicode编码解码在线转换工具 Unicode 是基于通用字符集(Universal Character Set)的标准来发展,并且同时也以书本的形式(The Unicode Standar ...

  4. android px,dp,sp大小转换工具

    package com.voole.playerlib.util; import android.content.Context; /** * Android大小单位转换工具类<br/> ...

  5. Json与javaBean之间的转换工具类

    /**  * Json与javaBean之间的转换工具类  *  * {@code 现使用json-lib组件实现  *    需要  *     json-lib-2.4-jdk15.jar  * ...

  6. Rsa加解密Java、C#、php通用代码 密钥转换工具

    之前发了一篇"TripleDes的加解密Java.C#.php通用代码",后面又有项目用到了Rsa加解密,还是在不同系统之间进行交互,Rsa在不同语言的密钥格式不一样,所以过程中主 ...

  7. ubuntu下编码转换工具

    ubuntu打开windows下的txt或者代码文件,经常会出现乱码, ubuntu自带一种转换工具,是命令行的,下面提供一种最简单的方法进行转换 比如要转换的文件为1.txt,进入1.txt的目录 ...

  8. 日期转换工具类 CommUtil.java

    package com.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.ut ...

  9. 视频转换工具 Transmageddon

    点这里 Transmageddon 是一个采用 Python 语言开发的视频转换工具,支持输出几乎所有的视频格式,同时也可以生成指定平台下的视频格式. 软件界面如下图所示

随机推荐

  1. PS教程:如何批量处理图片

    1.我们先准备两个文件夹,一个用来装你要处理的图片,可以是几百上千张,另一个是空文件夹,用来装等下处理好的图片. 2.打开PS,打开未处理文件夹里的任何一张图片. 3. 在红圈中点击,新建一个动作. ...

  2. 安装Cuda9.0+cudnn7.3.1+tensorflow-gpu1.13.1

    我的安装版本: win10 x64 VS2015 conda python 3.7 显卡 GTX 940mx Cuda 9.0 cudnn v7.3.1 Tensorflow-gpu 1.13.1 1 ...

  3. 纯HTML和CSS实现点击切换

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Nginx的介绍和安装详解

    [介绍+安装]Nginx的介绍和安装详解   == 介绍和安装 == Nginx是一个自由.开源.高性能及轻量级的HTTP服务器及反转代理服务器, 其性能与IMAP/POP3代理服务器相当.Nginx ...

  5. activiti_change_ok

    package com.xinwei.process.controller; import java.util.Calendar; import java.util.HashMap; import j ...

  6. MYSQLi数据访问查询数据

    单条件查询 <body> <div align="center" style="width:90%;"> <h1>数据查询& ...

  7. DeepNetwork---tensorflow实现

    https://github.com/zle1992/Reinforcement_Learning_Game DeepQNetwork.py import numpy as np import ten ...

  8. Discuz! 安装模板、插件提示“对不起,您安装的不是正版应用...

    iscuz 社区在更新到2.0以上后,增加了对插件的版本检测,在安装时,可能会出现:“对不起,您安装的不是正版应用,安装程序无法继续执行”的提示,要解决这个其实挺容易的,找到以下文件: /source ...

  9. over(partition by)开窗函数的使用

    开窗函数是分析函数中的一种,开窗函数与聚合函数的区别是:开窗函数是用于计算基于组的某种聚合值且每个的组的聚合计算结果可以有多行,而聚合函数每个组的聚合计算结果只有一个.使用开窗函数可以在没有group ...

  10. 20165215 预备作业3 Linux安装及学习

    Linux安装 根据老师的链接,我VirtualBox下载的是5.2.6的版本,下载Ubuntu时使用老师的链接总是出现404 Not found的页面,于是我采用其它方式下载了16.04.3的版本 ...