Apache组织开发了一套用于操作JavaBean的API,这套API考虑到了很多实际开发中的应用场景,因此在实际开发中很多程序员使用这套API操作JavaBean,以简化程序代码的编写。
BeanUtils的作用:
1)支持String到8种基本数据类型的转换;
2)其他引用数据类型都需要注册转换器:ConvertUtils.register(Converter,Class);
public class Person {
private String name;
private String password;
private Integer age;
private Date birthday;
getter/setter
}
采用beanutils设置属性
@Test
public void test1() throws Exception {
Person person = new Person();
BeanUtils.setProperty(person, "name", "zhangsan");
System.out.println(person.getName());
}
采用beanutils拷贝对象的属性(基本类型)
@Test
public void test2() throws Exception {
String name = "zhangsan";
String password = "123";
String age = "34"; Person person = new Person();
BeanUtils.setProperty(person, "name", name);
BeanUtils.setProperty(person, "password", password);
BeanUtils.setProperty(person, "age", age);// 自动类型转换,只支持8种基本数据类型 System.out.println(person);
}
采用beanutils拷贝对象的属性(非基本类型)
@Test
public void test3() throws Exception {
String name = "zhangsan";
String password = "123";
String age = "34";
String birthday = "1980-09-05"; Person person = new Person();
BeanUtils.setProperty(person, "name", name);
BeanUtils.setProperty(person, "password", password);
BeanUtils.setProperty(person, "age", age);// 自动类型转换
BeanUtils.setProperty(person, "birthday", birthday); // 类型转换失败
System.out.println(person);
}
为让Date能够赋值到birthday属性上,我们给beanutils注册一个日期转换器。
改进代码如下:
@Test
public void test4() throws Exception {
String name = "zhangsan";
String password = "123";
String age = "34";
String birthday = "1980-09-05"; 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 (null == str || "".equals(str.trim())) {
return null;
} SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
return format.parse(str);
} catch (ParseException e) {
throw new ConversionException(e);
}
} }, Date.class); Person person = new Person();
BeanUtils.setProperty(person, "name", name);
BeanUtils.setProperty(person, "password", password);
BeanUtils.setProperty(person, "age", age);// 自动类型转换
BeanUtils.setProperty(person, "birthday", birthday);
System.out.println(person);
}
采用beanutils自带的日期类型转换器
@Test
public void test5() throws Exception {
String name = "zhangsan";
String password = "123";
String age = "34";
String birthday = "1980-09-05"; ConvertUtils.register(new DateLocaleConverter(), Date.class); Person person = new Person();
BeanUtils.setProperty(person, "name", name);
BeanUtils.setProperty(person, "password", password);
BeanUtils.setProperty(person, "age", age);// 自动类型转换
BeanUtils.setProperty(person, "birthday", birthday);
System.out.println(person);
}
收集数据到map中,并通过beanutils拷贝到指定的对象中去
@Test
public void test6() throws Exception { Person person = new Person(); ConvertUtils.register(new DateLocaleConverter(), Date.class); Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "aa");
map.put("password", "123");
map.put("age", 23);
map.put("birthday", "1980-09-05"); // 用map集合中的值,填充bean的属性,
// 注意:属性名称必须要一致
BeanUtils.populate(person, map); System.out.println(person);
}