• 对象转换利器之Dozer


    在Java的世界中,经常会涉及到需要在2个对象中进行转换,比如说: 

    • 调用SOAP Web服务,需要把自己的Domain对象转换为Soap服务的Jaxb对象请求,
    • 在分层级SOA架构中,2个层级之间Domain对象的转换,
    • 在分布式系统中,每个模块使用自己的领域对象,防止自己的业务被其他模块的domain对象影响


    通常情况下,程序员们会自己弄一个Transformer层,来做对象之间的转换。估计很多同志在自己的项目代码中都可以看到Transformer的身影。但是,复杂的Java对象之间的转换并不是一个非常容易的问题,因为你需要不停的检测某个属性值是否为null,你需要做属性类型的转换,Date对应到String, String对应到Int等等。这就导致了Transformer的臃肿以及对程序员生产力的浪费。Dozer就是对懒(you)惰(xiu)程序员在这个问题上的通用解决方案。 

    Dozer是一个开源的,通用的,健壮的,灵活的,易于重用的,可配置的Java对象Mapping框架 。这个定义中的每一个形容词对程序员都非常亲切. (作者注: Dozer虽好,请不要贪恋, 因为任何对象之间的转换都会带来维护和性能的开销,能不做对象转换还是不要做了,跑不掉的时候,请选用Dozer)。 

    Dozer怎么用 
    使用Dozer的流程非常清楚: 

    • 配置你需要mapping的类,以及类之间的属性之间的对应关系。
    • 根据你的配置初始化Mapper
    • 使用mapper.map(source, target.class)获取转换后的对象。

    Dozer提供了2种方式去配置需要mapping的类,一种是基于XML,一种是基于API,API的方式是5.3版本才开始提供的,主要是为了解决xml方式不可动态修改mapping配置的问题。下面来看一个最基础的使用Dozer的例子: 

    假设在一个系统中,模块A中客户的类型是User,而模块B中客户的类型是Customer, 现在模块B需要接受模块A中客户,并转换为自己模块的客户类型Customer。 

    Java代码  收藏代码
    1. public class User {  
    2.     private int userId;  
    3.     private String  userName;  
    4.   
    5.     public User(int userId, String userName) {  
    6.         this.userId = userId;  
    7.         this.userName = userName;  
    8.     }  
    9.   
    10.     public int getUserId() {  
    11.         return userId;  
    12.     }  
    13.   
    14.     public void setUserId(int userId) {  
    15.         this.userId = userId;  
    16.     }  
    17.   
    18.     public String getUserName() {  
    19.         return userName;  
    20.     }  
    21.   
    22.     public void setUserName(String userName) {  
    23.         this.userName = userName;  
    24.     }  
    25. }  
    26.   
    27. public class Customer {  
    28.   
    29.     private int customerId;  
    30.     private String customerName;  
    31.   
    32.     public int getCustomerId() {  
    33.         return customerId;  
    34.     }  
    35.   
    36.     public String getCustomerName() {  
    37.         return customerName;  
    38.     }  
    39.   
    40.     public void setCustomerId(int customerId) {  
    41.         this.customerId = customerId;  
    42.     }  
    43.   
    44.     public void setCustomerName(String customerName) {  
    45.         this.customerName = customerName;  
    46.     }  
    47. }  


    配置User和Customer属性的之间的对应关系 

    Java代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <mappings xmlns="http://dozer.sourceforge.net"  
    3.           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.           xsi:schemaLocation="http://dozer.sourceforge.net  
    5.           http://dozer.sourceforge.net/schema/beanmapping.xsd">dia  
    6.     <mapping>  
    7.         <class-a>com.xianlinbox.dozer.source.User</class-a>  
    8.         <class-b>com.xianlinbox.dozer.destination.Customer</class-b>  
    9.         <field>  
    10.             <a>userId</a>  
    11.             <b>customerId</b>  
    12.         </field>  
    13.         <field>  
    14.             <a>userName</a>  
    15.             <b>customerName</b>  
    16.         </field>  
    17.     </mapping>  
    18. </mappings>  


    调用Mapper获取转换后的对象: 

    Java代码  收藏代码
    1. public class DozerMapperTest {  
    2.     private DozerBeanMapper mapper;  
    3.   
    4.     @Before  
    5.     public void setUp() throws Exception {  
    6.         mapper = new DozerBeanMapper();  
    7.         mapper.setMappingFiles(Arrays.asList("dozer-bean-mappings.xml"));  
    8.     }  
    9.   
    10.     @Test  
    11.     public void testMapping() throws Exception {  
    12.         User user = new User(1, "user1");  
    13.         Customer customer = mapper.map(user, Customer.class);  
    14.         assertThat(customer.getCustomerId(),equalTo(1));  
    15.         assertThat(customer.getCustomerName(),equalTo("user1"));  
    16.     }  
    17. }  



    Dozer做对象转换有什么特点 

    • 默认属性Mapping,即如果属性名称一样, 就不需要显示配置,Dozer会自动处理。
    • 自动做类型转换,即如果两个属性名称一样,即使类型不一样,在Dozer可理解范围内,帮你处理得妥妥的。Dozer可理解的类型范围非常广,这会极大的提升程序员的生产力
    • 不必担心中间的null property,遇到null property,Dozer会把对应的所有属性全部设置为null,而不会抛NullPointerExeception。



    Dozer可理解的类型转换有: 
    • 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 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. 

    Dozer官网地址:http://dozer.sourceforge.net/

    转至:http://ningandjiao.iteye.com/blog/1989838

  • 相关阅读:
    linux命令行
    mybatis中#{}和${}的区别
    @InitBinder的作用
    mui 实用封装销毁页面
    【SQLite】简单的基本使用步骤
    常用的一些操作方法
    【HttpWeb】Post和GET请求基本封装
    【接口验证】特性验证参数
    小谈单例模式
    vs下开端口直接调试iis
  • 原文地址:https://www.cnblogs.com/nightswatch/p/4546376.html
Copyright © 2020-2023  润新知