Commons beanutils组件简介
作者:CharlieChenBlog 时间:2023-11-28 07:13:27
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组件的相关内容网站的支持!
来源:http://blog.csdn.net/tobetheender/article/details/52830814
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# 执行CMD命令并接收返回结果的操作方式
springboot整合mybatisplus的方法详解
![](https://img.aspxhome.com/file/2023/9/125559_0s.png)
Java类成员访问权限控制知识总结
![](https://img.aspxhome.com/file/2023/5/96075_0s.png)
SpringMVC使用RESTful接口案例详解
java中Struts2文件上传问题详解
误将.idea文件提交至git后删除的操作方法
java 定时同步数据的任务优化
Android WebView实现长按保存图片及长按识别二维码功能
![](https://img.aspxhome.com/file/2023/6/139336_0s.gif)
WinForm下 TextBox只允许输入数字的小例子
基于Spring Boot使用JpaRepository删除数据时的注意事项
![](https://img.aspxhome.com/file/2023/2/83072_0s.jpg)
C#中datagridview的EditingControlShowing事件用法实例
C#中后台post请求常用的两种方式总结
![](https://img.aspxhome.com/file/2023/6/104266_0s.png)
springboot 如何配置多个jndi数据源
![](https://img.aspxhome.com/file/2023/1/110811_0s.png)
搭建一个基础的Resty项目框架
SpringCloud Feign参数问题及解决方法
Treeview动态添加用户控件传值和取值的实例代码
![](https://img.aspxhome.com/file/2023/5/102245_0s.jpg)
android图库播放幻灯片时按power键灭屏再亮屏显示keyguard
Android应用中制作选中后图标变大浮动效果的代码分享
![](https://img.aspxhome.com/file/2023/5/137135_0s.jpg)
Android侧滑效果简单实现代码
![](https://img.aspxhome.com/file/2023/8/138448_0s.gif)