JavaBean之間拷貝利器-Dozer 如果現(xiàn)在還是原始的手動(dòng)拷貝或者使用BeanUtil 趕緊換吧
典型的解決方案就是手動(dòng)拷貝,弊端很明顯,代碼中充斥大量Set Get方法,真正的業(yè)務(wù)沒(méi)埋藏與值的拷貝之中。另一種方案就是使用BeanUtil,但BeanUtil不夠很好的靈活性,又時(shí)候還不得不手動(dòng)拷貝。
對(duì)于這種重復(fù)沒(méi)有實(shí)際意義的拷貝難道沒(méi)有終結(jié)解決方案嗎?! Dozer 一把JavaBean利器。(http://dozer.sourceforge.net/)
什么是Dozer?
看看官方的定義:
another. Typically, these Java Beans will be of different complex types.
Dozer supports simple property mapping, complextype mapping, bi-directional mapping, implicitexplicit mapping, as well as recursive
mapping.This includes mapping collection attributes that also need mapping at the element level.
如何使用呢? 類似BeanUtil 很簡(jiǎn)單,如果兩個(gè)javaBean之間,屬性名相同,類型為基本類型,那么下面代碼就OK了,基本沒(méi)有學(xué)習(xí)曲線。
DestinationObject destObject = mapper.map(sourceObject, DestinationObject.class);
or
DestinationObject destObject = new DestinationObject();
mapper.map(sourceObject, destObject);
posted on 2009-04-12 22:11 advincenting 閱讀(3169) 評(píng)論(10) 編輯 收藏