当前位置: 移动技术网 > IT编程>开发语言>Java > Commons beanutils组件简介

Commons beanutils组件简介

2019年07月19日  | 移动技术网IT编程  | 我要评论

肖臻杰,李茂图片,墙体保温

        commons beanutils是apache开源组织提供的用于操作java bean的工具包。使用commons beanutils,我们可以很方便的对bean对象的属性进行操作。今天为大家介绍一下该包的常用方法。

1.什么是beanutils

        程序中对javabean的操作很频繁, 所以apache提供了一套开源的api,方便对javabean的操作,即beanutils组件。

2.beanutils的作用

        简化javabean的操作。 

       在一般的写bean组件的时候,都必须要写setter和getter方法,当然假如我们事先已经知道bean的相关属性和方法,写bean是比较简单的。

3.beanutils依赖包
        用户可以从www.apache.org下载beanutils组件,然后再在项目中引入jar文件。

(1) beanutils相关包
commons-beanutils-1.8.3.jar
commons-beanutils-1.8.3-javadoc.jar
commons-beanutils-1.8.3-javadoc.jar
commons-beanutils-bean-collections-1.8.3.jar
commons-beanutils-core-1.8.3.jar

(2) logic4j相关包
commons-logging.jar
log4j.jar
注:如果缺少日志jar文件,报错:

java.lang.noclassdeffounderror: org/apache/commons/logging/logfactory 
at org.apache.commons.beanutils.convertutilsbean.(convertutilsbean.java:157) 
at org.apache.commons.beanutils.beanutilsbean.(beanutilsbean.java:117) 
at org.apache.commons.beanutils.beanutilsbean$1.initialvalue(beanutilsbean.java:68) 
at

二:实例—基本用法

用法1: 对象属性的拷贝

beanutils.copyproperty(admin, "username", "jack");
beanutils.setproperty(admin, "age", 18);

用法2:对象的拷贝

beanutils.copyproperties(newadmin, admin);

用法3: map数据拷贝到javabean中
注意:map中的key要与javabean的属性名称一致

beanutils.populate(adminmap, map);

代码举例

javabean类

package com.beanutils.test;
import java.util.date;
/**
 * 1. bean类设计
 * @author charlie.chen
 *
 */
public class admin {
 private int id;
 private string username;
 private string pwd;
 private int age;
 private date birth;
 public date getbirth() {
 return birth;
 }
 public void setbirth(date birth) {
 this.birth = birth;
 }
 public int getage() {
 return age;
 }
 public void setage(int age) {
 this.age = age;
 }
 public string getpwd() {
 return pwd;
 }
 public void setpwd(string pwd) {
 this.pwd = pwd;
 }
 public int getid() {
 return id;
 }
 public void setid(int id) {
 this.id = id;
 }
 public string getusername() {
 return username;
 }
 public void setusername(string username) {
 this.username = username;
 }
 @override
 public string tostring() {
 return "admin [age=" + age + ", birth=" + birth + ", id=" + id
 + ", pwd=" + pwd + ", username=" + username + "]";
 } 
}

通过beanutils对javabean的基本操作   

@test
 public void test1() throws exception {
 // a. 基本操作
 admin admin = new admin();
 //admin.setusername("charlie.chen");
 //admin.setpwd("999");
 // b. beanutils组件实现对象属性的拷贝
 beanutils.copyproperty(admin, "username", "jack");
 beanutils.setproperty(admin, "age", 18);
 // 总结1: 对于基本数据类型,会自动进行类型转换!
 // c. 对象的拷贝
 admin newadmin = new admin();
 beanutils.copyproperties(newadmin, admin);
 // d. map数据,拷贝到对象中
 admin adminmap = new admin();
 map<string,object> map = new hashmap<string,object>();
 map.put("username", "jerry");
 map.put("age", 29);
 // 注意:map中的key要与javabean的属性名称一致
 beanutils.populate(adminmap, map);
 // 测试
 system.out.println(adminmap.getusername());
 system.out.println(adminmap.getage());
 }

三:实例—日期类型的拷贝

需要注册日期类型转换器,2种方式参见下面代码:   

 //1.自定义日期类型转换器
 @test
 public void test2() throws exception {
 // 模拟表单数据
 string name = "jack";
 string age = "20";
 string birth = " ";
 // 对象
 admin admin = new admin();
 // 注册日期类型转换器:1, 自定义的方式
 convertutils.register(new converter() {
 // 转换的内部实现方法,需要重写
 @override
 public object convert(class type, object value) {
 // 判断
 if (type != date.class) {
 return null;
 }
 if (value == null || "".equals(value.tostring().trim())) {
 return null;
 }
 try {
 // 字符串转换为日期
 simpledateformat sdf = new simpledateformat("yyyy-mm-dd");
 return sdf.parse(value.tostring());
 } catch (parseexception e) {
 throw new runtimeexception(e);
 }
 }
 },date.class);
 // 把表单提交的数据,封装到对象中
 beanutils.copyproperty(admin, "username", name);
 beanutils.copyproperty(admin, "age", age);
 beanutils.copyproperty(admin, "birth", birth);
 //------ 测试------
 system.out.println(admin);
 }
 //2. 使用提供的日期类型转换器工具类
 @test
 public void test3() throws exception {
 // 模拟表单数据
 string name = "jack";
 string age = "20";
 string birth = null;
 // 对象
 admin admin = new admin();
 // 注册日期类型转换器:2, 使用组件提供的转换器工具类
 convertutils.register(new datelocaleconverter(), date.class);
 // 把表单提交的数据,封装到对象中
 beanutils.copyproperty(admin, "username", name);
 beanutils.copyproperty(admin, "age", age);
 beanutils.copyproperty(admin, "birth", birth);
 //------ 测试------
 system.out.println(admin);
 }

总结

        以上所述是小编给大家介绍的commons beanutils组件的相关内容,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网