BeanUtils 是阿帕奇不满意sun公司的内省类操作太麻烦所以自己开发了简便的操作JavaBean属性的类-BeanUtils,所有代码都是在网易云课堂上看的30天轻松掌握JavaWeb
- 下载BeanUtils开发包,怎么下载网上都有,因为时间原因,我就不介绍了
划横线的应该可有可无,具体我没试,如果有人知道的话希望给我留言, 谢谢。 - 先建一个JavaBean 取名为Person
package com.test.beanutils;
import java.util.Date;
public class Person { //JavaBean
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;
}
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;
}
}
- 使用BeanUtils操作JavaBean
package com.test.beanutils;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
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;
public class Demo1 {
}
这是整个类的大概,因为要给每个方法写注释,所以就把它们拿出来了,可以把下面的方法写入demo1类里面
@Test
public void test1() throws IllegalAccessException, InvocationTargetException {
Person p = new Person();
BeanUtils.setProperty(p, "name", "nia");
System.out.println(p.getName());
}
setProperty有三个参数,第一个是待操作JavaBean的对象,第二个是JavaBean对象里面的某个属性名,第三个是我们需要给那个属性赋的值;
@Test
public void test2() throws IllegalAccessException, InvocationTargetException {
Person p = new Person();
String name = "abcd";
String password = "1234";
String age = "23";
BeanUtils bu = new BeanUtils();
BeanUtils.setProperty(p, "name", name);
bu.setProperty(p, "age", age); //age虽然是String类型的,但是Person的age是int类型的
//BuanUtils会自动进行类型转换,但是BeanUtils默认只支持
//Java基础的8种类型(如:int, double, 等);
bu.setProperty(p, "password", password);
System.out.println(p.getName() +" " + p.getPassword() + " " + p.getAge());
}
这个方法表示了Beanutils赋值会进行自动转换,但只是基础类型的转换。
//让BeanUtils转换除了基础的8大类型,(如Data)
@Test
public void test3() throws IllegalAccessException, InvocationTargetException {
Person p = new Person();
String name = "abcd";
String password = "1234";
String age = "23";
String birthday = "1996-1-26";
//注册一个类型转换器
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class type, Object value) {
if (value == null) {
return null;
}
if (!(value instanceof String)) {
throw new ConversionException("lz只支持String类型转换");
}
String str = (String) value;
if (str.trim().equals("")) {
return null;
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return df.parse(str);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}, Date.class);
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName() +" " + p.getPassword() + " " + p.getAge());
System.out.println(p.getBirthday());
}
这个方法是说明怎么让BeanUtils转换成除了基础的8大类型之外的类型,比如说Date类型。
要先注册一个转换器,其中用到了 匿名类。ConvertUtils.register里的第一个参数是new 一个Convertor对象,同时写出Convertor中转化的过程就是改怎幺转换。第二个参数是需要转化的类型,比如上面就是需要转换成Date,so写Date.class;
@Test
public void test4() throws IllegalAccessException, InvocationTargetException {
Person p = new Person();
String name = "aaaa";
String password = "1234";
String age = "23";
String birthday = "1996-12-24";
ConvertUtils.register(new DateLocaleConverter(), Date.class);
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName() +" " + p.getPassword() + " " + p.getAge());
Date date = p.getBirthday();
System.out.println(date.toLocaleString());
}
这个方法是注册BeanUtils自己所带的转换其他类型的转换器,但是这个有个bug,就是如果上面的String birthday = “1996-12-24”;这条语句变成String birthday = “”;这个语句,运行的时候会出错,但是向我们test3就不会。
@Test
public void test5() throws IllegalAccessException, InvocationTargetException {
Map<String, String> map = new HashMap<String, String>();
map.put("name", "bbbb");
map.put("password", "2334");
map.put("age", "23");
map.put("birthday", "1234-12-3");
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person bean = new Person();
BeanUtils.populate(bean, map); //用map集合填充bean的属性
System.out.println(bean.getName());
System.out.println(bean.getPassword());
System.out.println(bean.getAge());
System.out.println(bean.getBirthday());
}
这个方法使用的时候要注意map的key 一定要跟JavaBean中的属性同名。