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<String ,String > map=new HashMap<String, String>();
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());

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青年IT男

您的打赏就是对我的肯定!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值