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使用案例的更多相关文章

  1. BeanUtil工具类的使用

    BeanUtils的使用 1.commons-beanutils的介绍 commons-beanutils是Apache组织下的一个基础的开源库,它提供了对Java反射和内省的API的包装,依赖内省, ...

  2. [转]BeanUtil使用

    BeanUtils的使用 转载自:https://blog.csdn.net/xxf159797/article/details/53645722 1.commons-beanutils的介绍 com ...

  3. java 内省综合案例和Beanutils工具包

    演示用eclipse自动生成 ReflectPoint类的setter和getter方法. 直接new一个PropertyDescriptor对象的方式来让大家了解JavaBean API的价值,先用 ...

  4. 15、Jdbc的优化(BeanUtils组件)

    Jdbc的优化! BeanUtils组件 自定义一个持久层的框架 DbUtils组件 案例优化 1. BeanUtils组件 1.1    简介 程序中对javabean的操作很频繁, 所以apach ...

  5. 20160406javaweb 之JDBC简单案例

    前几天写的user注册登录注销案例,没有用到数据库,现在做出改动,使用数据库存储信息: 一.首先我们需要建立一个数据库: 如下图: 创建数据库的代码如下: -- 导出 database02 的数据库结 ...

  6. 第13天 JSTL标签、MVC设计模式、BeanUtils工具类

    第13天 JSTL标签.MVC设计模式.BeanUtils工具类 目录 1.    JSTL的核心标签库使用必须会使用    1 1.1.    c:if标签    1 1.2.    c:choos ...

  7. 第14天dbutils与案例

    第14天dbutils与案例 第14天dbutils与案例    1 1.    1.dbutils介绍    2 2.    2.dbutils快速入门    2 3.    3.dbutils A ...

  8. Web开发模式【Mode I 和Mode II的介绍、应用案例】

    开发模式的介绍 在Web开发模式中,有两个主要的开发结构,称为模式一(Mode I)和模式二(Mode II) 首先我们来理清一些概念吧: DAO(Data Access Object):主要对数据的 ...

  9. BeanUtils 读取数据

    前两篇文章都是关于setProperty的,下面来说一个关于getProperty 的小案例.如下: MyClass.java package beanutils; public class MyCl ...

随机推荐

  1. Struts之 拦截器配置 ( Interceptor )

    struts2体系结构的核心就是拦截器. 以链式执行,对真正要执行的方法(execute())进行拦截.首先执行Action配置的拦截器,在Action和Result执行之后,拦截器再一次执行(与先前 ...

  2. jQuery——类的添加与删除

    添加类:addClass 删除类:removeClass 判断类是否存在:hasClass <!DOCTYPE html> <html lang="en"> ...

  3. html——导航demo

    通过行内块.伪类对导航栏进行设置 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  4. Java程序员2016年终总结

    回顾2016年, 很庆幸,自己能在2016年年尾找到一份满意的web后台开发工作.这也是我学习编程以来第一份开发工作,我很是珍惜. 还记得大三接触了Java的JFrame编写的坦克大战之后,就对编程产 ...

  5. (转) Hibernate检索方式概述

    http://blog.csdn.net/yerenyuan_pku/article/details/70554816 Hibernate检索方式概述 我们在对数据库的操作中,最常用的是select, ...

  6. (转)Hadoop入门进阶课程

    http://blog.csdn.net/yirenboy/article/details/46800855 1.Hadoop介绍 1.1Hadoop简介 Apache Hadoop软件库是一个框架, ...

  7. Java 之jdbc连接mysql数据库

    package jdbc; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; ...

  8. java的四舍五入及取整

    四舍五入用 Math.round(double a): 向上取整用 Math.ceil(double a): 向下取整用 Math.floor(double a):

  9. Problem 29

    Problem 29 Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: 仔细看看以下a与b的组合 22=4, 2 ...

  10. 小白神器 - Django - 起步

    小白神器 - Django - 起步 一.  Django下载 1. 命令行 pip install django==1.11.16 pip install django==1.11.16 -i ht ...