Commons Beanutils是Apache開源組織提供的用于操作JAVA BEAN的工具包。使用commons beanutils,我們可以很方便的對(duì)bean對(duì)象的屬性進(jìn)行操作。今天為大家介紹一下該包的常用方法。
1.什么是BeanUtils
程序中對(duì)javabean的操作很頻繁, 所以apache提供了一套開源的api,方便對(duì)javabean的操作,即BeanUtils組件。
2.BeanUtils的作用
簡(jiǎn)化javabean的操作。
在一般的寫bean組件的時(shí)候,都必須要寫setter和getter方法,當(dāng)然假如我們事先已經(jīng)知道bean的相關(guān)屬性和方法,寫bean是比較簡(jiǎn)單的。
3.BeanUtils依賴包
用戶可以從www.apache.org下載BeanUtils組件,然后再在項(xiàng)目中引入jar文件。
(1) BeanUtils相關(guān)包
commons-beanutils-1.8.3.jar
commons-beanutils-1.8.3-javadoc.jar
commons-beanutils-1.8.3-javadoc.jar
commons-beanutils-bean-collections-1.8.3.jar
commons-beanutils-core-1.8.3.jar
(2) Logic4j相關(guān)包
commons-logging.jar
log4j.jar
注:如果缺少日志jar文件,報(bào)錯(cuò):
1
2
3
4
5
|
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.apache.commons.beanutils.ConvertUtilsBean.(ConvertUtilsBean.java: 157 ) at org.apache.commons.beanutils.BeanUtilsBean.(BeanUtilsBean.java: 117 ) at org.apache.commons.beanutils.BeanUtilsBean$ 1 .initialValue(BeanUtilsBean.java: 68 ) at |
二:實(shí)例—基本用法
用法1: 對(duì)象屬性的拷貝
1
2
|
BeanUtils.copyProperty(admin, "userName" , "jack" ); BeanUtils.setProperty(admin, "age" , 18 ); |
用法2:對(duì)象的拷貝
1
|
BeanUtils.copyProperties(newAdmin, admin); |
用法3: map數(shù)據(jù)拷貝到j(luò)avabean中
注意:map中的key要與javabean的屬性名稱一致
1
|
BeanUtils.populate(adminMap, map); |
代碼舉例
javabean類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package com.beanutils.test; import java.util.Date; /** * 1. bean類設(shè)計(jì) * @author Charlie.chen * */ public class Admin { private int id; private String userName; private String pwd; private int age; private Date birth; public Date getBirth() { return birth; } public void setBirth(Date birth) { this .birth = birth; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this .pwd = pwd; } public int getId() { return id; } public void setId( int id) { this .id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this .userName = userName; } @Override public String toString() { return "Admin [age=" + age + ", birth=" + birth + ", id=" + id + ", pwd=" + pwd + ", userName=" + userName + "]" ; } } |
通過(guò)BeanUtils對(duì)javabean的基本操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@Test public void test1() throws Exception { // a. 基本操作 Admin admin = new Admin(); //admin.setUserName("Charlie.chen"); //admin.setPwd("999"); // b. BeanUtils組件實(shí)現(xiàn)對(duì)象屬性的拷貝 BeanUtils.copyProperty(admin, "userName" , "jack" ); BeanUtils.setProperty(admin, "age" , 18 ); // 總結(jié)1: 對(duì)于基本數(shù)據(jù)類型,會(huì)自動(dòng)進(jìn)行類型轉(zhuǎn)換! // c. 對(duì)象的拷貝 Admin newAdmin = new Admin(); BeanUtils.copyProperties(newAdmin, admin); // d. map數(shù)據(jù),拷貝到對(duì)象中 Admin adminMap = new Admin(); Map<String,Object> map = new HashMap<String,Object>(); map.put( "userName" , "Jerry" ); map.put( "age" , 29 ); // 注意:map中的key要與javabean的屬性名稱一致 BeanUtils.populate(adminMap, map); // 測(cè)試 System.out.println(adminMap.getUserName()); System.out.println(adminMap.getAge()); } |
三:實(shí)例—日期類型的拷貝
需要注冊(cè)日期類型轉(zhuǎn)換器,2種方式參見下面代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
//1.自定義日期類型轉(zhuǎn)換器 @Test public void test2() throws Exception { // 模擬表單數(shù)據(jù) String name = "jack" ; String age = "20" ; String birth = " " ; // 對(duì)象 Admin admin = new Admin(); // 注冊(cè)日期類型轉(zhuǎn)換器:1, 自定義的方式 ConvertUtils.register( new Converter() { // 轉(zhuǎn)換的內(nèi)部實(shí)現(xiàn)方法,需要重寫 @Override public Object convert(Class type, Object value) { // 判斷 if (type != Date. class ) { return null ; } if (value == null || "" .equals(value.toString().trim())) { return null ; } try { // 字符串轉(zhuǎn)換為日期 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" ); return sdf.parse(value.toString()); } catch (ParseException e) { throw new RuntimeException(e); } } },Date. class ); // 把表單提交的數(shù)據(jù),封裝到對(duì)象中 BeanUtils.copyProperty(admin, "userName" , name); BeanUtils.copyProperty(admin, "age" , age); BeanUtils.copyProperty(admin, "birth" , birth); //------ 測(cè)試------ System.out.println(admin); } //2. 使用提供的日期類型轉(zhuǎn)換器工具類 @Test public void test3() throws Exception { // 模擬表單數(shù)據(jù) String name = "jack" ; String age = "20" ; String birth = null ; // 對(duì)象 Admin admin = new Admin(); // 注冊(cè)日期類型轉(zhuǎn)換器:2, 使用組件提供的轉(zhuǎn)換器工具類 ConvertUtils.register( new DateLocaleConverter(), Date. class ); // 把表單提交的數(shù)據(jù),封裝到對(duì)象中 BeanUtils.copyProperty(admin, "userName" , name); BeanUtils.copyProperty(admin, "age" , age); BeanUtils.copyProperty(admin, "birth" , birth); //------ 測(cè)試------ System.out.println(admin); } |
總結(jié)
以上所述是小編給大家介紹的Commons BeanUtils組件的相關(guān)內(nèi)容,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://blog.csdn.net/tobetheender/article/details/52830814