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());


}
}

   

代码測试效果图:




posted on 2017-07-03 21:53  blfbuaa  阅读(209)  评论(0编辑  收藏  举报