首页 > 代码库 > BeanUtils使用案例

BeanUtils使用案例

 1.BeanUtils框架/工具(APACHE开源组织开发)
   (1)BeanUtils框架能够完成内省的一切功能,而且优化
   (2)BeanUtils框架能够对String<->基本类型自动转化(即八种基本类型的转换)
   (3)BeanUtils框架自定义转换器:
ConvertUtils.register( 转换规则 ,目标对象的Class)
   (4)向BeanUtils框架注册自定义转换器必须放在bu.setProperty()代码之前    
   (5)使用BeanUtils内置String->Date的转换器:

ConvertUtils.register(new DateLocaleConverter(),java.util.Date.class);

  (6)要使用的两个jar包:
commons-beanutils-1.8.0.jar和commons-logging.jar

     将这两个包复制到MyEclipse或者Eclipse中后,鼠标放在jar包上,右键选择“Build Path”-->"Add to Build Path"即可在代码中用这两个jar包了(并且这样代码中才有提示)

2.代码练习:


Student的代码(Student.java):


package cn.wwh.www.java.beanutils;


import java.util.Date;


/**
 *类的作用:特别注意:开始将birthday写出birthDay,而在beanutils中仍然用的是birthday,此时程序不能通过,
 *getBirthday和setBirthday中的D改过来后,才运行成功,这完全符合之前的说的,框架和setXxx和getXxx有关而与private Date birthay;中的birthDay无关
 *
 *
 *@author 一叶扁舟
 *@version 1.0
 *@创建时间: 2014-7-21   上午11:49:19
 */
public class Student {


private String name;
private int age;
private Date birthday;

public Student(){
System.out.println("构造函数");
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the birthDay
*/
public Date getBirthday() {
return birthday;
}
/**
* @param birthDay the birthDay to set
*/
public void setBirthday(Date birthday) {
this.birthday = birthday;
}

}

BeanUtils的测试代码(Demo1.java):


package cn.wwh.www.java.beanutils;


import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;


/**
 *类的作用:因为BeanUtil只能转换八种基本类型,如果转换其他类型,
 *则需要自己定义转换方式, 例如:想转换日期类型
 * 
 * 
 *@author 一叶扁舟
 *@version 1.0
 *@创建时间: 2014-7-21 上午11:49:07
 */
public class Demo {


// 自定一个类型转化器(将String---->Date格式化的输出)
@Test
public void testDemo1() throws Exception {
Student student = new Student();
BeanUtils bu = new BeanUtils();

//一定要先注册下
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class calzz, Object type) {
/**
* 参数1:class,java.uitl.Date;(目标类型) 参数2:传入参数的类型,java.lang.string;
*/
String strBirthday = (String) type;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");


try {
return sdf.parse(strBirthday);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}, Date.class);


bu.setProperty(student, "name", "一叶扁舟");
bu.setProperty(student, "age", "22");
bu.setProperty(student, "birthday", "2014-07-22");


// 取出数据
String name = bu.getProperty(student, "name");
String age = bu.getProperty(student, "age");
String birthday = bu.getProperty(student, "birthday");


System.out.println("name:" + name + "\nage:" + age + "\nbirthday:"
+ new Date(birthday).toLocaleString());


/*
* 输出结果: 构造函数 
* name:一叶扁舟
*  age:22 
*  birthday:2014-7-22 14:00:00(我并没有修改时间,与里面源代码调用有关)
*/


}
@Test
public void testDemo2() throws Exception{
Student student = new Student();
BeanUtils bu = new BeanUtils();

ConvertUtils.register(new DateLocaleConverter(), java.util.Date.class);



bu.setProperty(student, "name", "一叶扁舟");
bu.setProperty(student, "age", "22");
bu.setProperty(student, "birthday", "2014-07-22");


// 取出数据
String name = bu.getProperty(student, "name");
String age = bu.getProperty(student, "age");
String birthday = bu.getProperty(student, "birthday");


System.out.println("name:" + name + "\nage:" + age + "\nbirthday:"
+ new Date(birthday).toLocaleString());


}
}

   

代码测试效果图:




BeanUtils使用案例