org.dozer浅析

news/2024/7/4 1:20:18 标签: import, string, encoding, class, apache, 工具
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">

本文出自:http://blog.csdn.net/hongchangfirst 

dozer是一种JavaBean的映射工具,类似于apache的BeanUtils。但是dozer更强大,它可以灵活的处理复杂类型之间的映射。不但可以进行简单的属性映射、复杂的类型映射、双向映射、递归映射等,并且可以通过XML配置文件进行灵活的配置。 


大体来说它可以把一个对象转化为另一个对象,这两个对象之间会有一定的映射规则。

如你有两个类,

class ForeignName

{

private String firstName;

private String lastName;

}


class ChineseName

{

private String firstName;

private String lastName;

}

比如一个中文名字“Hongchang Zhang”,在ForeignName中

firstName="Hongchang";

lastName="Zhang";

但在中国我们的firstName应该是姓,也就是说firstName="Zhang",我们可以采用dozer的map规则进行映射,把ForeignName中的firstName映射为ChineseName的lastName。下述为nameMapper.xml

<?xml version="1.0" class="tags" href="/tags/ENCODING.html" title=encoding>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">  
         
       <mapping>  
        <class-a>ForeignName</class-a>  
        <class-b>ChineseName</class-b>  
        <field>  
            <a>firstName</a>  
            <b>lastName</b>  
        </field>  
        <field>  
            <a>lastName</a>  
            <b>firstName</b>  
        </field>  
       </mapping>  
 </mappings>  

上边的映射很容易看明白,接下来我们看看具体怎么映射:

ForeignName hc_z = new ForeignName();  
hc_z.firstName="Hongchang";

hc_z.lastName="Zhang";

DozerBeanMapper mapper = new DozerBeanMapper();  
List<String> mappers = new ArrayList<String>();  
mappers.add("nameMapper.xml");  
mapper.setMappingFiles(mappers);  
ChineseName z_hc = new ChineseName();  
mapper.map(hc_z, z_hc);  
System.out.println(z_hc.firstName+ " " + z_hc.lastName);  


可以看到,很自然的我们得到了"Zhang Hongchang",当然我们要包含以下包:

class="tags" href="/tags/IMPORT.html" title=import>import java.util.ArrayList;  
class="tags" href="/tags/IMPORT.html" title=import>import java.util.List;  
  
class="tags" href="/tags/IMPORT.html" title=import>import org.apache.commons.logging.Log;  
class="tags" href="/tags/IMPORT.html" title=import>import org.apache.commons.logging.LogFactory;  
class="tags" href="/tags/IMPORT.html" title=import>import org.dozer.DozerBeanMapper;

有人可能会说 了,为什么要这么麻烦呢?当然这个例子可能有点杀鸡用牛刀了,但是它的好处在于自动化映射,而不是你程序自己去映射。


http://www.niftyadmin.cn/n/748300.html

相关文章

postgrepsql学习

目录1、postgresql----索引失效2、postgrep查看sql是否使用索引3、模糊查询like优化4、postgrep自增主键5、on CONFLICT语法6、Postgrep使用json字段&#xff0c;存储json数据7、postgrep json字段查询1、postgresql----索引失效 https://www.cnblogs.com/alianbog/p/5648455.…

Java在Windows平台上调用进程不传参的问题解决办法

本文出自:http://blog.csdn.net/hongchangfirst 当我们用Java调用exe的时候&#xff0c;执行了exe并没有得到输出结果&#xff0c;而在dos下该exe可以正确运行&#xff0c; 如果该exe有以下特点&#xff1a; 1输入参数是一个文件或多个。 2输出结果到一个文件或多个。 那么…

Spring Cloud Feign 请求添加headers

目录方案一&#xff1a;方法上的RequestMapping注解添加headers信息方案二&#xff1a;接口上的RequestMapping注解添加headers信息方案三&#xff1a;使用Headers注解添加headers信息方案四&#xff1a;自定义RequestInterceptor添加headers信息方案五&#xff1a;自定义Reque…

Eclipse导入到web项目没有run on server

本文出自:http://blog.csdn.net/hongchangfirst 由于以前的项目都是用myeclipse开发的&#xff0c;现在要换成eclipse来开发。但是项目导入到eclipse中发现该项目并不是web项目&#xff0c;也不能部署到tomcat里面去。 现在解决了这个问题了。 一.请首先确保你的机器上…