Dozer实现对象间拷贝

项目中,经常会遇到各层对象之间相互进行值传递的过程,如在数据据持久层有一持久类ClassA,在视图层可能会变为ViewA,通常情况下,如果两个类结构一样,最常使用的是BeanUtils.copyProperties(src,aim)方法将一个对象的值赋给另一个对象!但是如果属性不同或者名称不同,则需要Dozer来完成,通过灵活的配置,达到不同对象间的拷贝! 

如下: 
一个XML配置文件:dozerBeanMapping.xml

[xml]  view plain copy
  1.  <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mappings PUBLIC "-//DOZER//DTD MAPPINGS//EN"  
  3. "http://dozer.sourceforge.net/dtd/dozerbeanmapping.dtd">  
  4. <mappings>  
  5.     <!-- <class-a>指定所要复制的源对象,<class-b>复制的目标对象,<a>源对象的属性名, <b>目标对象的属性名。  
  6.     wildcard默认为true,在此时默认对所有属性进行map,如果为false,则只对在xml文件中配置的属性进行map。 -->  
  7.     <configuration>  
  8.         <stop-on-errors>false</stop-on-errors>  
  9.         <date-format>MM/dd/yyyy HH:mm</date-format>  
  10.         <wildcard>true</wildcard>  
  11.     </configuration>  
  12.     <mapping>  
  13.         <class-a>com.njusc.view.BoxView</class-a>  
  14.         <class-b>com.njusc.view.DepView</class-b>  
  15.         <field>  
  16.             <a>boxId</a>  
  17.             <b>depId</b>  
  18.         </field>  
  19.         <field>  
  20.             <a>depName</a>  
  21.             <b>depName</b>  
  22.         </field>  
  23.     </mapping>  
  24. </mappings>  



下面两个简单的类

[java]  view plain copy
  1. package com.njusc.view;  
  2.   
  3. public class BoxView  
  4. {   
  5.     private String boxId;        
  6.     private String depName;  
  7.       
  8.     public String getBoxId() {  
  9.         return boxId;  
  10.     }  
  11.     public void setBoxId(String boxId) {  
  12.         this.boxId = boxId;  
  13.     }  
  14.     public String getDepName() {  
  15.         return depName;  
  16.     }  
  17.     public void setDepName(String depName) {  
  18.         this.depName = depName;  
  19.     }   
  20. }  


[java]  view plain copy
  1. package com.njusc.view;  
  2.   
  3. public class DepView {  
  4.     private String depId;  
  5.     private String depName;  
  6.     public String getDepId() {  
  7.         return depId;  
  8.     }  
  9.     public void setDepId(String depId) {  
  10.         this.depId = depId;  
  11.     }  
  12.     public String getDepName() {  
  13.         return depName;  
  14.     }  
  15.     public void setDepName(String depName) {  
  16.         this.depName = depName;  
  17.     }  
  18. }  



下面看具体的操作:

[java]  view plain copy
  1. package com.njusc.util;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.dozer.DozerBeanMapper;  
  7.   
  8. import com.njusc.view.BoxView;  
  9. import com.njusc.view.DepView;  
  10.   
  11. public class DozerBeanMapperUtil {  
  12.     public static void main(String[] args) {  
  13.         BoxView bv = new BoxView();  
  14.         bv.setBoxId("1234");  
  15.         bv.setDepName("测试");  
  16.           
  17.         DozerBeanMapper mapper=new DozerBeanMapper();  
  18.         BoxView newbv = new BoxView();  
  19.         //mapper.map(bv, newbv); //这种方式或下面的方式都可以  
  20.         newbv = (BoxView)mapper.map(bv, BoxView.class);  
  21.         System.out.println(newbv.getDepName()+newbv.getBoxId());  
  22.           
  23.         DozerBeanMapper mapper1=new DozerBeanMapper();  
  24.         List myMappingFiles = new ArrayList();  
  25.         myMappingFiles.add("dozerBeanMapping.xml");  
  26.         mapper1.setMappingFiles(myMappingFiles);  
  27.           
  28.         DepView dep = new DepView();  
  29.              //不同对象间拷贝  
  30.         mapper1.map(bv, dep);  
  31.         System.out.println(dep.getDepId()+dep.getDepName());  
  32.     }  
  33. }  

通过mapper.setMappingFiles()设置映射文件,可以添加多个配置文件,也可以把所有的映射写在一个配置文件里面。  更多复杂例子请见它自带的doc。

dozer-5.0.jar (155.1 KB)包下载地址:http://dl.javaeye.com/topics/download/320009d9-b368-3f77-aaf0-7cc8e4c0fadd

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值