生活随笔
收集整理的這篇文章主要介紹了
BeanUtils
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄 前言 推薦文章 Maven依賴 常用API 常見使用場景 日期類型的拷貝 封裝工具類
前言
Apache BeanUtils是一種方便我們對JavaBean進行操作的工具類。
阿里巴巴java開發手冊(泰山版):
從上圖中可以看出實際開發中我們應選擇 Spring BeanUtils,一方面不需要額外引入依賴包,另一方面性能會比Apache BeanUtils 性能好(同理的還有 Spring StringUtils),所以【本文內容僅做了解】!!!
推薦文章
不考慮性能,從拓展性角度看看!BeanUtils還是有很多問題的!
復制對象時字段類型不一致,導致賦值不上,你怎么解決?自己拓展? 復制對象時字段名稱不一致,例如CarPo里叫carName,CarVo里叫name,導致賦值不上,你怎么解決?自己拓展? 如果是集合類的復制,例如List<CarPo>轉換為List<CarVo>,你怎么處理? (省略一萬字…)
【如何優雅的轉換Bean對象】
【Java 實體映射工具 MapStruct】
Maven依賴
< dependency> < groupId> commons-beanutils
</ groupId> < artifactId> commons-beanutils
</ artifactId> < version> 1.9.4
</ version>
</ dependency>
常用API
public void copyProperties
( Object dest
, Object orig
)
public Map describe ( Object bean
)
public void populate
( Object bean
, Map map
)
public void setProperty ( Object bean
, String name
, Object value
)
public String getProperty ( Object bean
, String name
)
常見使用場景
User user1
= new User ( ) ;
user1
. setName ( "張三" ) ;
User user2
= new User ( ) ;
BeanUtils . copyProperties ( user2
, user1
) ;
UserForm userForm
= new UserForm ( ) ;
User user
= new User ( ) ;
BeanUtils . copyProperties ( user
, userForm
) ;
User user
= new User ( ) ;
Map userMap
= BeanUtils . describe ( user
) ; Map userMap
= new HashMap ( ) ;
User user
= new User ( ) ;
BeanUtils . populate ( user
, userMap
) ;
日期類型的拷貝
BeanUtils.copyProperties支持Date類型: 【轉】BeanUtils.copyProperties支持Date類型
import java. lang. reflect. InvocationTargetException ;
import org. apache. commons. beanutils. BeanUtils ;
import org. apache. commons. beanutils. ConvertUtils ;
import org. apache. log4j. Logger ;
public class BeanUtilsEx extends BeanUtils { private static Logger logger
= Logger . getLogger ( BeanUtilsEx . class ) ; static { ConvertUtils . register ( new DateConvert ( ) , java. util. Date. class ) ; ConvertUtils . register ( new DateConvert ( ) , String . class ) ; } public static void copyProperties ( Object target
, Object source
) { try { org. apache. commons. beanutils. BeanUtils. copyProperties ( target
, source
) ; } catch ( IllegalAccessException | InvocationTargetException e
) { logger
. error ( "擴展BeanUtils.copyProperties支持data類型:" + e
. getMessage ( ) ) ; e
. printStackTrace ( ) ; } }
}
import java. text. DateFormat ;
import java. text. ParseException ;
import java. text. SimpleDateFormat ;
import java. util. Date ;
import org. apache. commons. beanutils. Converter ;
public class DateConvert implements Converter { @Override public Object convert ( Class class1
, Object value
) { if ( value
== null ) { return null ; } if ( value
instanceof Date ) { return value
; } if ( value
instanceof Long ) { Long longValue
= ( Long ) value
; return new Date ( longValue
. longValue ( ) ) ; } if ( value
instanceof String ) { String dateStr
= ( String ) value
; Date endTime
= null ; try { String regexp1
= "([0-9]{4})-([0-1][0-9])-([0-3][0-9])T([0-2][0-9]):([0-6][0-9]):([0-6][0-9])" ; String regexp2
= "([0-9]{4})-([0-1][0-9])-([0-3][0-9])(/t)([0-2][0-9]):([0-6][0-9]):([0-6][0-9])" ; String regexp3
= "([0-9]{4})-([0-1][0-9])-([0-3][0-9])" ; if ( dateStr
. matches ( regexp1
) ) { dateStr
= dateStr
. split ( "T" ) [ 0 ] + " " + dateStr
. split ( "T" ) [ 1 ] ; DateFormat sdf
= new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ; endTime
= sdf
. parse ( dateStr
) ; return endTime
; } else if ( dateStr
. matches ( regexp2
) ) { DateFormat sdf
= new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ; endTime
= sdf
. parse ( dateStr
) ; return endTime
; } else if ( dateStr
. matches ( regexp3
) ) { DateFormat sdf
= new SimpleDateFormat ( "yyyy-MM-dd" ) ; endTime
= sdf
. parse ( dateStr
) ; return endTime
; } else { return dateStr
; } } catch ( ParseException e
) { e
. printStackTrace ( ) ; } } return value
; }
}
封裝工具類
把請求中的參數拷貝到javaBean對象中
public static < T > T requestToBean ( HttpServletRequest request
, Class < T > clazz
) { Object obj
= null ; try { obj
= clazz
. newInstance ( ) ; } catch ( Exception e
) { e
. printStackTrace ( ) ; throw new RuntimeException ( e
) ; } Enumeration < String > enu
= request
. getParameterNames ( ) ; while ( enu
. hasMoreElements ( ) ) { String name
= enu
. nextElement ( ) ; String value
= request
. getParameter ( name
) ; try { BeanUtils . setProperty ( obj
, name
, value
) ; } catch ( Exception e
) { e
. printStackTrace ( ) ; throw new RuntimeException ( e
) ; } } return ( T ) obj
;
}
總結
以上是生活随笔 為你收集整理的BeanUtils 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。