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使用案例的更多相关文章
- BeanUtil工具类的使用
BeanUtils的使用 1.commons-beanutils的介绍 commons-beanutils是Apache组织下的一个基础的开源库,它提供了对Java反射和内省的API的包装,依赖内省, ...
- [转]BeanUtil使用
BeanUtils的使用 转载自:https://blog.csdn.net/xxf159797/article/details/53645722 1.commons-beanutils的介绍 com ...
- java 内省综合案例和Beanutils工具包
演示用eclipse自动生成 ReflectPoint类的setter和getter方法. 直接new一个PropertyDescriptor对象的方式来让大家了解JavaBean API的价值,先用 ...
- 15、Jdbc的优化(BeanUtils组件)
Jdbc的优化! BeanUtils组件 自定义一个持久层的框架 DbUtils组件 案例优化 1. BeanUtils组件 1.1 简介 程序中对javabean的操作很频繁, 所以apach ...
- 20160406javaweb 之JDBC简单案例
前几天写的user注册登录注销案例,没有用到数据库,现在做出改动,使用数据库存储信息: 一.首先我们需要建立一个数据库: 如下图: 创建数据库的代码如下: -- 导出 database02 的数据库结 ...
- 第13天 JSTL标签、MVC设计模式、BeanUtils工具类
第13天 JSTL标签.MVC设计模式.BeanUtils工具类 目录 1. JSTL的核心标签库使用必须会使用 1 1.1. c:if标签 1 1.2. c:choos ...
- 第14天dbutils与案例
第14天dbutils与案例 第14天dbutils与案例 1 1. 1.dbutils介绍 2 2. 2.dbutils快速入门 2 3. 3.dbutils A ...
- Web开发模式【Mode I 和Mode II的介绍、应用案例】
开发模式的介绍 在Web开发模式中,有两个主要的开发结构,称为模式一(Mode I)和模式二(Mode II) 首先我们来理清一些概念吧: DAO(Data Access Object):主要对数据的 ...
- BeanUtils 读取数据
前两篇文章都是关于setProperty的,下面来说一个关于getProperty 的小案例.如下: MyClass.java package beanutils; public class MyCl ...
随机推荐
- hibernate.cfg.xml配置
hibernate.hbm2ddl.auto 配置: create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这 ...
- [ SCOI 2007 ] Perm
\(\\\) \(Description\) 给出只包括多个\(0\text~ 9\)的数字集,求有多少个本质不同的全排列,使得组成的数字能够整除\(M\). \(|S|\in [1,10]\),\( ...
- DeadObjectException
开发的过程中有时候会遇到DeadObjectException,说明系统service已经停止运行,解决的方式是在mainfistxml的application标签中添加android:hardwar ...
- 《Java编程的逻辑》第二部分 面向对象
- [Windows Server 2008] Serv-U安装方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:Serv- ...
- CNN结构:图片风格分类效果已成(StyleAI)
CNN结构:图片风格分类效果已成.可以在色彩空间对图片风格进行分类,并进行目标分类. StyleAI构架:FasterRCnn + RandomTrees 为何不使用MaskRCNN? MaskRCN ...
- css特殊效果
border-radius实现特殊形状 .box{ width: 100px; height: 100px; background: orange; border: 1px solid #000; b ...
- cordova插件分类
1.android自动更新功能所需插件 cordova plugin add https://github.com/whiteoctober/cordova-plugin-app-version.gi ...
- 阿里云ECS远程桌面连接失败
管理=>本实例安全组=>配置规则=>配置3389端口
- QQ空间里写的开发心得
不回头看一眼还真没发现我已经写过这么多开发心得日志. 理一理设备数据走向 https://user.qzone.qq.com/1156740846/blog/1522292793 action的生命 ...