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. nginx下如何配置 ssl证书?腾讯云ssl证书为例!

    nginx下如何配置 ssl证书?腾讯云ssl证书为例! 目前为止,https已经成为一种趋势,想要开启https就需要ssl证书. 首先,为域名注册ssl证书. 腾讯云注册地址:https://cl ...

  2. JS——隐式全局变量

    在函数中,var声明的是局部变量,不带var的是隐式全局变量 <script> function fn() { var a = b = c = 0;//a是局部变量,b.c是全局变量 va ...

  3. CSS——text-indent

    在h1标签里套入a标签并写上文字,有利于seo,但是文字如何隐藏呢?一般都是a标签变成内联块并首行缩进为负值. <!DOCTYPE html> <html lang="en ...

  4. 重绘DataGridView的DataGridViewCheckBoxCell控件

    最近项目中要用到在DataGridView单元格里面放置一个带有文本的 DataGridViewCheckBoxCell控件但原有 的是不支持的然后我就想着重写个 DataGridViewCheckB ...

  5. ProE常用曲线方程:Python Matplotlib 版本代码(玫瑰曲线)

    Pyplot教程:https://matplotlib.org/gallery/index.html#pyplots-examples 玫瑰曲线 文字描述 平面内,围绕某一中心点平均分布整数个正弦花瓣 ...

  6. Cookie的实现

    Cookie是web server下发给浏览器的任意的一段文本,在后续的http 请求中,浏览器会将cookie带回给Web Server.同时在浏览器允许脚本执行的情况下,Cookie是可以被Jav ...

  7. zabbix实现163邮件报警

    Zabbix 邮件报警 电脑登录网易邮箱配置,把自己的授权码看一下,并写入配置文件 server端安装配置邮件服务器 [root@server ~]# yum -y install mailx dos ...

  8. mapbox-gl 使用ArcGISServer 发布的栅格切片

    最近使用mapbox 进行数据化展现.刚好用到了超图平台在去三维系统,顺带就用超图平台发布了栅格切片,用来做底图,但是超图平台是试用的许可,栅格切片有SuperMap 的水印,实在不雅观. 在网上搜索 ...

  9. R 安装car包失败

    在RStudio里安装car包的时候报错 /usr/bin/ld: cannot find -llapack /usr/bin/ld: cannot find -lblas make: *** [qu ...

  10. BZOJ 2501 [usaco2010 Oct]Soda Machine

    [题意概述] 给出一个[0,1,000,000,000]的整数数轴,刚开始每个位置都为0,有n个区间加操作,最后询问数轴上最大的数是多少. [题解] 我写的是离散化后线段树维护区间最值. 其实貌似不用 ...