|
Posted on 2008-11-01 00:03 J2EE Home工作室 閱讀(484) 評(píng)論(0) 編輯 收藏 所屬分類: 好用的組件
Common BeanUtils組件方便了對(duì)JavaBean的使用。其中的一些類方法,使我們使用JavaBean得到了便利。
使用Common BeanUtils組件需要三個(gè)Jar包,分別是
commons-beanutils-1.8.0-BETA.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.1.jar
可從官網(wǎng)下載,不過為了方便,我把三個(gè)包傳上來。點(diǎn)擊下載
下面用四個(gè)例子說明該組件的三個(gè)優(yōu)點(diǎn)。
例子一:
創(chuàng)建三個(gè)Java文件,分別為
Address.java
Profile.java
User.java
在寫一個(gè)類文件遞進(jìn)調(diào)用函數(shù),命名:BeanUtilsExample1.java,源碼:
 Code
package com.sy;

import java.util.Map;
import java.util.HashMap;
import java.util.GregorianCalendar;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

 public class BeanUtilsExample1 {
 private User prepareData() {
Profile profile = new Profile();
profile.setEmail("shiyangxt@126.com");
profile.setBirthDate(new GregorianCalendar(3212, 9, 10).getTime());
Map<String, String> phone = new HashMap<String, String>();
phone.put("home", "11011011");
phone.put("office", "82826905");
profile.setPhone(phone);
 Address[] address = { new Address("中國(guó)", "北京", "100120", "天安門北大街888號(hào)"),
new Address("中國(guó)", "廣州", "100120", "石牌村666號(hào)") };
profile.setAddress(address);

User user = new User();
user.setUserId(new Long(123456789));
user.setUsername("shiyang");
user.setPassword("12345");
user.setProfile(profile);
return user;
}

 public static void main(String[] args) {
BeanUtilsExample1 example = new BeanUtilsExample1();
User user = example.prepareData();
 try {
System.out.println("輸出對(duì)象的屬性值---------------------------------");
System.out.println(BeanUtils.getProperty(user, "userId"));
System.out.println(BeanUtils.getProperty(user, "username"));//返回字符型
System.out.println(PropertyUtils.getProperty(user, "username"));//返回對(duì)象類型
System.out.println(BeanUtils.getProperty(user, "profile.email"));//重點(diǎn)
System.out
.println(BeanUtils.getProperty(user, "profile.birthDate"));//重點(diǎn)
System.out.println(BeanUtils.getProperty(user,
"profile.phone(home)"));//重點(diǎn)
System.out.println(BeanUtils.getProperty(user,
"profile.phone(office)"));//重點(diǎn)
System.out.println(BeanUtils.getProperty(user,
"profile.address[0].city"));//重點(diǎn)
System.out.println(BeanUtils.getProperty(user,
"profile.address[1].city"));//重點(diǎn)

User user2 = new User();
BeanUtils.copyProperties(user2, user);
//兩層拷貝,基本類型復(fù)制值,對(duì)于引用類型(除String,封裝類型外)復(fù)制地址值。
System.out.println("輸出復(fù)制屬性的屬性值-------------------------------");
System.out.println(BeanUtils.getProperty(user, "username"));
System.out
.println(BeanUtils.getProperty(user, "profile.birthDate"));//重點(diǎn)
System.out.println(BeanUtils.getProperty(user,
"profile.phone(home)"));//重點(diǎn)
System.out.println(BeanUtils.getProperty(user,
"profile.address[0].addr"));//重點(diǎn)

System.out.println("輸出復(fù)制屬性修改以后的屬性值---------------------");
BeanUtils.setProperty(user2, "userId", new Long(8888888));
PropertyUtils.setProperty(user2, "username", "ahah");
BeanUtils.setProperty(user2, "profile.email", "shiyangxt@126.com");//重點(diǎn)
BeanUtils.setProperty(user2, "profile.birthDate",//重點(diǎn)
new GregorianCalendar(1900, 2, 5).getTime());
BeanUtils.setProperty(user2, "profile.address[0]", new Address(
"中國(guó)", "深圳", "600600", "深北大道111號(hào)"));//重點(diǎn)
System.out.println(BeanUtils.getProperty(user2, "userId"));
System.out.println(BeanUtils.getProperty(user2, "username"));
System.out.println(BeanUtils.getProperty(user2, "profile"));
System.out.println(BeanUtils.getProperty(user2, "profile.email"));//重點(diǎn)
System.out.println(BeanUtils
.getProperty(user2, "profile.birthDate"));//重點(diǎn)
System.out.println(BeanUtils.getProperty(user2,
"profile.address[0].city"));//重點(diǎn)

System.out.println("與被復(fù)制屬性值的對(duì)象的比較-------------------------------");
System.out.println(BeanUtils.getProperty(user, "userId"));
System.out.println(BeanUtils.getProperty(user, "username"));
System.out.println(BeanUtils.getProperty(user, "profile"));
System.out.println(BeanUtils.getProperty(user, "profile.email"));//重點(diǎn)
System.out
.println(BeanUtils.getProperty(user, "profile.birthDate"));//重點(diǎn)
System.out.println(BeanUtils.getProperty(user,
"profile.address[0].city"));//重點(diǎn)
 } catch (Exception e) {
e.printStackTrace();
}
}
}
從中可以看出,
調(diào)用一個(gè)屬性中的方法,只需要加一個(gè)“.”即可。精簡(jiǎn)了操作。
還可以拷貝屬性,但要注意是二層拷貝。
還要注意BeanUtils和PropertyUtils的區(qū)別。
例子二:
動(dòng)態(tài)創(chuàng)建屬性
文件名:BeanUtilsExample2,源碼:
 Code
package com.sy;

import java.util.GregorianCalendar;
import org.apache.commons.beanutils.LazyDynaBean;
import org.apache.commons.beanutils.BeanUtils;

 public class BeanUtilsExample2 {
//動(dòng)態(tài)創(chuàng)建屬性
 public static void main(String args[]) throws Exception {

LazyDynaBean hh = new LazyDynaBean();
hh.set("country", "中國(guó)");
hh.set("city", "北京");
hh.set("postCode", "100120");
hh.set("addr", "aaaaaaa");

LazyDynaBean bb = new LazyDynaBean();
bb.set("phone", "home", "11011011");
bb.set("phone", "office", "111111");
bb.set("email", "sh@126.com");
bb.set("address", 0, hh);
bb.set("birthDate", new GregorianCalendar(1990, 3, 29).getTime());

LazyDynaBean tt = new LazyDynaBean();
tt.set("userId", new Long(8888888));
tt.set("gggg", "施楊");
tt.set("password", "sgsgsgsg");
tt.set("dddd", bb);

System.out.println(BeanUtils.getProperty(tt, "gggg"));
System.out.println(BeanUtils.getProperty(tt, "dddd.birthDate"));
System.out.println(BeanUtils.getProperty(tt,
"dddd.address[0].addr"));
System.out
.println(BeanUtils.getProperty(tt, "dddd.phone(office)"));
}
}

例子三:
連接Mysql數(shù)據(jù)庫(kù)
文件名BeanUtilsExample3.java源碼:
 Code
package com.sy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.ResultSetDynaClass;

 public class BeanUtilsExample3 {
 public static void main(String args[]) throws Exception {
Connection conn = getConnection();
PreparedStatement ps = conn
.prepareStatement("select id,title,time from guestbook2 order by id desc");
ResultSet rs = ps.executeQuery();

ResultSetDynaClass rsdc = new ResultSetDynaClass(rs);//重點(diǎn),二次封裝,對(duì)連接對(duì)象有依賴
Iterator itr = rsdc.iterator();
 while (itr.hasNext()) {
DynaBean bean = (DynaBean) itr.next();
System.out.print(bean.get("id") + "\t");
System.out.print(bean.get("title") + "\t");
System.out.println(bean.get("time"));
}
conn.close();
}

 private static Connection getConnection() {
String url = "jdbc:mysql://localhost:3306/guestbook";
String username = "root";
String password = "hicc";
Connection conn = null;
 try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
 } catch (ClassNotFoundException e) {
e.printStackTrace();
 } catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
例子四:
文件BeanUtilsExample4.java,源碼:
 Code
package com.sy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.RowSetDynaClass;

 public class BeanUtilsExample4 {
 public static void main(String args[]) throws Exception {
Connection conn = getConnection();
PreparedStatement ps = conn
.prepareStatement("select id,title,time from guestbook2 order by id desc");
ResultSet rs = ps.executeQuery();

RowSetDynaClass rsdc = new RowSetDynaClass(rs);
//重點(diǎn),與ResultSetDynaClass的區(qū)別
conn.close();//重點(diǎn),關(guān)閉連接后仍能讀取
Iterator itr = rsdc.getRows().iterator();
 while (itr.hasNext()) {
DynaBean bean = (DynaBean) itr.next();
System.out.print(bean.get("id") + "\t");
System.out.print(bean.get("title") + "\t");
System.out.println(bean.get("time"));
}
}

 private static Connection getConnection() {
String url = "jdbc:mysql://localhost:3306/guestbook";
String username = "root";
String password = "hicc";
Connection conn = null;
 try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
 } catch (ClassNotFoundException e) {
e.printStackTrace();
 } catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
這就是Common BeanUtils組件的主要應(yīng)用!!!
|