[转]Java——BeanUtils基本用法 - 南阳客 - 博客园
https://www.cnblogs.com/nanyangke-cjz/p/7110217.html
Java——BeanUtils基本用法
为了操作JavaBean的属性,sun公司自己写了一套内省的api(在Java.beans.*)中,但是我们发现操作起来还是比较复杂的,所以apache公司就自己写了一套api替代了它,大大方便了开发者去调用,并大大的提高了效率。接下来我们通过几个demo来简单的熟悉一下这套api,首先你需要准备两个jar包(commons-logging.jar和org.apache.commons.beanutils.jar)。
1.创建一个简单的JavaBean Student
package com.day09;
import java.util.Date;
public class Student {
@Override
public String toString() {
return "Student [name=" + name + ", password=" + password + ", age=" + age + ", birthday=" + birthday + "]";
}
private String name;
private String password;
private int age;
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
protected String getNames() {
return name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2.通过junit的方式进行方法测试
package com.day09;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;
/**
* apache感觉Java的内省api写的太麻烦了,自己开发了一套api就是BeanUtils,大大的简化了内省操作Javabean的属性,提高了效率
*
* @author Administrator
*
*/
public class BeanUtilsDemo {
/**
* 使用BeanUtils来给属性设置
*
* @throws Exception
*/
@Test
public void setProperties() throws Exception {
Student s = new Student();
// 传入bean的对象,属性名,属性值即可
BeanUtils.setProperty(s, "age", 23);
System.out.println(s.getAge());
}
/**
* BeanUtils支持8中基本数据类型进行数据转换(从String转换到对应的八种基本数据类型)
*
* @throws Exception
*/
@Test
public void setPropertiesOnBaseDataType() throws Exception {
String name = "zhangsan";
String password = "123456";
String age = "23";
Student s = new Student();
BeanUtils.setProperty(s, "name", name);
BeanUtils.setProperty(s, "password", password);
BeanUtils.setProperty(s, "age", age);
System.out.println(s);
}
/**
* BeanUtis虽然支持8中基本类型,但是它可提供了注册转换器机制,可以让除8中基本类型之外的其他类型也可以自动转换
*
* @throws Exception
*/
@Test
public void setPropertiesOnDateType() throws Exception {
String birthday = "1993-9-25";
Student s = new Student();
// 为了让日期赋值到bean的属性上,我们需要给Date注册一个日期转换器
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class type, Object value) {
if (value == null) {
return null;
}
if (!(value instanceof String)) {
throw new ConversionException("只支持String类型");
}
String str = (String) value;
if ("".equals(str.trim())) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(str);
} catch (ParseException e) {
throw new RuntimeException(e);// 要带上e,因为异常链不能断
}
}
}, Date.class);
BeanUtils.setProperty(s, "birthday", birthday);
System.out.println(s.getBirthday());
}
/**
* BeanUtis虽然支持8中基本类型,但是它可提供了注册转换器机制,可以让除8中基本类型之外的其他类型也可以自动转换
*
* @throws Exception
*/
@Test
public void setPropertiesOnDateType2() throws Exception {
Student s = new Student();
String birthday = "1993-09-25";
// 为了让日期赋值到bean的属性上,我们需要给Date注册一个日期转换器,而BanUtils框架也为我们提供了一个日期转换器
ConvertUtils.register(new DateLocaleConverter(Locale.CHINESE, "yy-MM-dd"), Date.class);
BeanUtils.setProperty(s, "birthday", birthday);
System.out.println(s.getBirthday());
}
/**
* 从map集合上做数据的映射
*
* @throws Exception
* @throws IllegalAccessException
*/
@Test
public void setPropertiesOnMap() throws IllegalAccessException, Exception {
Student s = new Student();
Map<String, String> map = new HashMap<String, String>();
// 这里同样要注册一个日期转换器
ConvertUtils.register(new DateLocaleConverter(Locale.CHINESE, "yy-MM-dd"), Date.class);
map.put("name", "zhangsan");
map.put("age", "23");
map.put("birthday", "1993-9-25");
BeanUtils.populate(s, map);
System.out.println(s);
}
}
至此,我们已将常见的几种通过反射获得类中字段的方法演示完毕,有不足的地方,希望大家多多提意见!
« 上一篇: Java内省
» 下一篇: tomcat的虚拟目录映射常用的几种方式
posted @ 2017-07-03 11:23 南阳客 阅读(343) 评论(0) 编辑 收藏
转摘文章,如有侵权,请联系删除。
本文详细介绍Apache的BeanUtils库,用于简化JavaBean属性操作。通过示例演示如何使用BeanUtils进行属性设置、数据类型转换及从Map集合映射数据。
8513

被折叠的 条评论
为什么被折叠?



