BeanUtils的使用
BeanUtils apache开发的为了简化对bean的开发的一套API下面就对开发中常用的API进行演示
第一步:搭建BeanUtil所需的*.jar commons-beanutils-1.8.3.jar
commons-logging-1.1.1.jar 导入包后需要构建路径 见附件
第二步:编写一个bean Person.java
public class Person {
private int age;
private String name;
private String password;
private Date birthdiy;
public Date getBirthdiy() {
return birthdiy;
}
public void setBirthdiy(Date birthdiy) {
this.birthdiy = birthdiy;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
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 class BeanUtilsTest {
/*
* BeanUtils默认只支持8中基本数据类型 ,基本类型会自动转换为相应的bean类型
*
*/
@Test
public void test1() throws IllegalAccessException, InvocationTargetException
{
Person person=new Person();
BeanUtils.setProperty(person, "name","小张");
BeanUtils.setProperty(person, "age","20");
BeanUtils.setProperty(person, "password","1234");
System.out.println(person.getName());
System.out.println(person.getAge());
System.out.println(person.getPassword());
}
/**
* 自定义类型转换器
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
@Test
public void test2() throws IllegalAccessException, InvocationTargetException
{
//注册一个类型转换器
ConvertUtils.register(new Converter(){
public Object convert(Class type, Object value) {
//判断这个value是否为空
if(value==null)
{
return null;
}
//判断这个value是否是字符串类型
if(!(value instanceof String))
{
return null;
}
//判断这个value是否为空字符串
String str=(String)value;
if(str.trim().equals(""))
{
return null;
}
//满足上面条件就执行类型装换
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
try {
return dateFormat.parse(str);
} catch (ParseException e) {
throw new ConversionException("不能类型转换");
}
}
}, Date.class);
Person person=new Person();
BeanUtils.setProperty(person, "birthdiy", "2010-03-12");
Date date=person.getBirthdiy();
System.out.println(date.toLocaleString());
}
/**
* 使用BeanUtils中的类型转换器
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
@Test
public void test3() throws IllegalAccessException, InvocationTargetException
{
//第二个参数的意思是要被转换成什么类型
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person person=new Person();
BeanUtils.setProperty(person, "birthdiy", "2010-03-12");
Date date=person.getBirthdiy();
System.out.println(date.toLocaleString());
}
/*
* 使用Map来把数据封装到bean中
*
*/
@Test
public void test4() throws IllegalAccessException, InvocationTargetException
{
Map
map.put("age", "20");
map.put("name", "小王");
map.put("password", "");
map.put("birthdiy", "2010-10-12");
//第二个参数的意思是要被转换成什么类型
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person person=new Person();
//使用map集合去填充到bean中
BeanUtils.populate(person, map);
Date date=person.getBirthdiy();
System.out.println(date.toLocaleString());
System.out.println(person.getAge());
System.out.println(person.getName());
System.out.println(person.getPassword());
}
}