日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

对象转换利器之Dozer

發(fā)布時(shí)間:2024/9/5 综合教程 36 生活家
生活随笔 收集整理的這篇文章主要介紹了 对象转换利器之Dozer 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

什么是Dozer

Dozer是一個(gè)Java對象轉(zhuǎn)換工具,可以在JavaBean和JavaBean之間進(jìn)行遞歸數(shù)據(jù)復(fù)制,并且適應(yīng)不同復(fù)雜的類型。Dozer會(huì)直接將名稱相同的屬性進(jìn)行復(fù)制,屬性名不同或者有特殊的要求則可以在xml中進(jìn)行配置。

除了使用Dozer,當(dāng)然你還由其他選擇:

典型的解決方案就是手動(dòng)拷貝,弊端很明顯,代碼中充斥大量Set 和Get方法,真正的業(yè)務(wù)被埋藏值與值的拷貝之中。

另一種方案就是使用BeanUtil,但BeanUtil不夠很好的靈活性,有時(shí)候還不得不手動(dòng)拷貝。Dozer可以靈活的對對象進(jìn)行轉(zhuǎn)換,且使用簡單。

Dozer做對象轉(zhuǎn)換有什么特點(diǎn)

默認(rèn)屬性Mapping,即如果屬性名稱一樣, 就不需要顯示配置,Dozer會(huì)自動(dòng)處理。
自動(dòng)做類型轉(zhuǎn)換,即如果兩個(gè)屬性名稱一樣,即使類型不一樣,在Dozer可理解范圍內(nèi),幫你處理得妥妥的。Dozer可理解的類型范圍非常廣,這會(huì)極大的提升程序員的生產(chǎn)力
不必?fù)?dān)心中間的null property,遇到null property,Dozer會(huì)把對應(yīng)的所有屬性全部設(shè)置為null,而不會(huì)拋NullPointerExeception。

Dozer可以自動(dòng)做數(shù)據(jù)類型轉(zhuǎn)換。當(dāng)前,Dozer支持以下數(shù)據(jù)類型轉(zhuǎn)換(都是雙向的)(注:Primitive基本數(shù)據(jù)類型 , 后面帶Wrapper是包裝類,Complex Type是復(fù)雜類型。):

•   Primitive to Primitive Wrapper 
  原型(int、long等)和原型包裝類(Integer、Long) • Primitive to Custom Wrapper
  原型和定制的包裝 • Primitive Wrapper to Primitive Wrapper
  原型包裝類和包裝類 • Primitive to Primitive
  原型和原型 • Complex Type to Complex Type
  復(fù)雜類型和復(fù)雜類型 • String to Primitive
  字符串和原型 • String to Primitive Wrapper
  字符串和原型包裝類 • String to Complex Type if the Complex Type contains a String constructor
  字符串和有字符串構(gòu)造器的復(fù)雜類型(類) • String 到復(fù)雜類型 , 如果復(fù)雜類型包含一個(gè) String 類型的構(gòu)造器 • String to Map
  字符串和Map • Collection to Collection
  集合和集合 • Collection to Array
  集合和數(shù)組 • Map to Complex Type
  Map和復(fù)雜類型 • Map to Custom Map Type
  Map和定制Map類型 • 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
  這些時(shí)間相關(guān)的常見類可以互換: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.
  字符串和支持Date/Calendar的對象 • Objects containing a toString() method that produces a long representing time in (ms) to any supported Date/Calendar object.
  如果一個(gè)對象的toString()方法返回的是一個(gè)代表long型的時(shí)間數(shù)值(單位:ms),就可以和任何支持Date/Calendar的對象轉(zhuǎn)換。

在普通Java項(xiàng)目中使用Dozer

在pom.xml中增加依賴

<dependency>
  <groupId>net.sf.dozer</groupId>
  <artifactId>dozer</artifactId>
  <version>5.5.1</version>
</dependency>

使用Dozer進(jìn)行類轉(zhuǎn)換

public class Main {

    public static void main(String[] args) {
        DozerBeanMapper mapper = new DozerBeanMapper();
        UserDO userDO = new UserDO();
        userDO.setName("hollis");
        userDO.setAddress("hz");
        userDO.setAge(25);
        userDO.setCompanyId(1);
        userDO.setBirthday(new Date());
        userDO.setGender("male");
        userDO.setEducation("1");
        userDO.setNickName("hollis");
        userDO.setPoliticalStatus("3");
        UserVO userVO = (UserVO) mapper.map(userDO, UserVO.class);
        System.out.println(userVO);
    }
}

特殊的字段轉(zhuǎn)換
在使用mapper進(jìn)行轉(zhuǎn)換前,設(shè)置一個(gè)或多個(gè)mapping文件

List myMappingFiles = new ArrayList();    
myMappingFiles.add("dozer-mapping.xml");    
mapper.setMappingFiles(myMappingFiles);

配置dozer-mapping.xml文件

數(shù)據(jù)類型不一致,或者名稱不相同或者有級(jí)聯(lián)關(guān)系等情況下,可以通過文件dozer-mapping.xml來進(jìn)行定制Bean的轉(zhuǎn)換

<?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">
    <mapping>
        <class-a>com.hollis.lab.UserDO</class-a>
        <class-b>com.hollis.lab.UserVO</class-b>
    </mapping>
</mappings>

在JavaWeb項(xiàng)目中使用Dozer

在pom.xml中增加依賴

<dependency>
  <groupId>net.sf.dozer</groupId>
  <artifactId>dozer</artifactId>
  <version>5.5.1</version>
</dependency>

使用Spring集成dozer

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="baseMapper" class="org.dozer.spring.DozerBeanMapperFactoryBean">
        <property name="mappingFiles">
            <list>
                <value>classpath:mapping/dozer-mapping.xml</value>
            </list>
        </property>
    </bean> 
</beans>

使用baseMapper進(jìn)行Bean的轉(zhuǎn)換

@Autowired
private Mapper baseMapper;
private UserVO doToVo(UserDO userDO){
    if(userDO == null) return null;
    UserVO vo = baseMapper.map(userDO, UserVO.getClass());
    if(userDO.getCompanyId != null) getCompany(vo);
    return vo;
}

通過以上的代碼加配置,我們就實(shí)現(xiàn)了從DO轉(zhuǎn)換到VO的部分操作,之所以說是部分操作,是因?yàn)槲覀冊?code>dozer-mapping.xml并沒有做多余的配置,只是使用dozer將DO中和VO中共有的屬性轉(zhuǎn)換了過來。對于其他的類型不同或者名稱不同等的轉(zhuǎn)換可以參考官網(wǎng)例子通過設(shè)置dozer-mapping.xml文件來實(shí)現(xiàn)。

上面還有一個(gè)getCompany()沒有實(shí)現(xiàn)。這個(gè)方法其實(shí)就是通過companyId查詢出company實(shí)體然后在賦值給UserVO中的company屬性。

注意事項(xiàng)

1:

DestinationObject destObject =  mapper.map(sourceObject, DestinationObject.class);

DestinationObject必須有無參構(gòu)造方法,不然會(huì)報(bào)java.lang.NoSuchMethodException: com.net.vo.CompanyVo.<init>()錯(cuò)誤。

2:

SourceObject和DestinationObject中屬性必須可讀寫,比如提供公開的get或set方法,不然無法復(fù)制數(shù)據(jù)。其實(shí),轉(zhuǎn)化的過程是通過調(diào)用UserDO中的getter方法和UserVO中的setter方法來實(shí)現(xiàn)的。

3:

dozer正常輸出日志需要增加兩個(gè)jar包

slf4j-api和slf4j-simple,或者,slf4j-api和slf4j-log4j12

例如:

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.6.1</version>
</dependency>

參考資料

使用Dozer優(yōu)雅的將DO轉(zhuǎn)換成VO:http://www.hollischuang.com/archives/1613

Dozer使用簡記:http://tedhacker.top/2016/08/17/Dozer%E4%BD%BF%E7%94%A8%E7%AE%80%E8%AE%B0/

Dozer 使用小結(jié):https://www.jianshu.com/p/bf8f0e8aee23

總結(jié)

以上是生活随笔為你收集整理的对象转换利器之Dozer的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。