使用beanUtils操纵javabean

1.Person类

package cn.itcast.beanutils;

import java.util.Date;

public class Person { //javabean封装用户数据
private String name;//字段
private String password;//字段
private int age;//字段
private Date birthday;
public String getAb() {//属性
return null;
}
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;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}

}

2.Demo1类

package cn.itcast.beanutils;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
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;

//使用beanUtils操作bean的属性(第三方)
public class Demo1 {

@Test
public void test1() throws IllegalAccessException,
InvocationTargetException {

Person p = new Person();
BeanUtils.setProperty(p, "name", "xcc");

System.out.println(p.getName());
}

//下面的代码是有问题的,因为beanUtils框架只支持基本数据类型转换
@Test
public void test2() throws IllegalAccessException,
InvocationTargetException {

String name = "aaaa";
String password = "123";
String age = "34";
String birthday = "1980-09-09";

Person p = new Person();
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age);// 只支持8种基本数据类型
BeanUtils.setProperty(p, "birthday", birthday);

System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
System.out.println(p.getBirthday());
}

@Test
public void test3() throws IllegalAccessException,
InvocationTargetException {

String name = "aaaa";
String password = "123";
String age = "34";
String birthday = "1980-09-09";

// 为了让日期赋到bean的birthday属性上,我们给beanUtils注册一个日期转换器
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 (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);

Person p = new Person();
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age);// 只支持8种基本数据类型
BeanUtils.setProperty(p, "birthday", birthday);

System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
System.out.println(p.getBirthday());

}

@Test
public void test4() throws IllegalAccessException,
InvocationTargetException {

String name = "aaaa";
String password = "123";
String age = "34";
String birthday = "1980-09-09";

// 为了让日期赋到bean的birthday属性上,我们给beanUtils注册一个日期转换器
ConvertUtils.register(new DateLocaleConverter(), Date.class);

Person p = new Person();
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age);// 只支持8种基本数据类型
BeanUtils.setProperty(p, "birthday", birthday);

System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
System.out.println(p.getBirthday());

}

public void test5() throws IllegalAccessException, InvocationTargetException{
Map map=new HashMap();
map.put("name", "aaa");
map.put("password", "123");
map.put("age", "23");
map.put("birthday", "1980-09-09");
// 为了让日期赋到bean的birthday属性上,我们给beanUtils注册一个日期转换器
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());



}
}

原文地址:https://www.cnblogs.com/xiaohuihui123/p/4359055.html