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. Java—RequestMapping相关用法

    RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径.它有6个属性:1.value:指定请求的具体地址:valu ...

  2. 国内外知名IT科技博客

    国内 1.36氪(www.36kr.com): 目前国内做的最风生水起的科技博客,以介绍国内外互联网创业新闻为主的博客网站,自己建立有36Tree互联网创业融投资社区.36氪的名字源于元素周期 表的第 ...

  3. win32绘图基础

     获取设备环境句柄: (1)WM_PAINT消息中: PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd,&ps); EndPaint(hwnd,&ps ...

  4. html5——多媒体(二)

    基本方法 load() //重新加载视频 play() //播放 pause() //暂停 基本属性 currentTime //视频播放的当前进度. duration //视频的总时间 paused ...

  5. JS——i++与++i

    先赋值后自增: var i = 0; var n1 = i++; alert(i);//返回1 alert(n1);//返回0 先自增后赋值: var i = 0; var n1 = ++i; ale ...

  6. R语言图表

    条形图 在R语言中创建条形图的基本语法是 barplot(H, xlab, ylab, main, names.arg, col) H是包含在条形图中使用的数值的向量或矩阵 xlab是x轴的标签 yl ...

  7. javascript模块化编程(一)(http://www.ruanyifeng.com/blog/2012/10/javascript_module.html)

    Javascript模块化编程(一):模块的写法   随着网站逐渐变成"互联网应用程序",嵌入网页的Javascript代码越来越庞大,越来越复杂. 网页越来越像桌面程序,需要一个 ...

  8. js基本类型的包装对象

    var test = "test"; test.a = "hello"; console.log(test.a); 在JavaScript中,“一切皆对象”,数 ...

  9. 解读PTR_ERR,ERR_PTR,IS_ERR

    解读PTR_ERR,ERR_PTR,IS_ERR 看到了几个宏PTR_ERR,ERR_PTR,IS_ERR(其实是内联函数).还是不太明白,然后就google搜索了一下,搜出来的结果真是不让人满意,看 ...

  10. easyui 网址

    http://www.runoob.com/jeasyui/jeasyui-datagrid-datagrid23.html http://www.jeasyui.com http://fineui. ...