準確的說,是由於pojo沒法一招走天下或者說內外部隔離的緣由,因此有些時候,不得不在兩個bean之間作copy或者轉換映射。對於直接性的屬性拷貝beanutil以及可以知足大部分要求,可是若是遇到字段不一致或者須要二次處理的狀況下,就須要進行人工代碼處理了。並且這些重複除非經過某種方式管理起來,否則系統中會有大量的複製粘貼。java
週六的時候,一個同事說他們那邊使用的dozer,還挺好用的,因而看了下官方手冊http://dozer.sourceforge.net/dozer-user-guide.pdf,確實比較靈活,有點相似於mybatis之餘jdbc原生代碼。DEMO示例(實際使用中建議使用spring IOC方式,以及classpath加載,故非以教程式,而是直接可用的代碼爲例子)以下。spring
原bean:json
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:mybatis
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; } }
轉換映射文件:app
<?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配置文件:ide
<?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.SpringJUnit4Cla***unner; import org.springframework.test.context.transaction.TransactionConfiguration; import com.alibaba.fastjson.JSON; @RunWith(SpringJUnit4Cla***unner.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)); } }
輸出以下:ui
{"age":1,"notMatch":"notmatch","srcName":"name1"}
{"age":1}
{"age":1,"notMatch":"notmatch","srcName":"name1"}
{"age":1,"destName":"name1","dictName":"dict name1"}this