对JavaBean的操作,Java提供了一套自己的方法,但是用起来感觉不咋的,但是Apache自己出版了一套BeanUtilsAPI方法进行Bean操作,很牛逼,很好用,下面放上几个小例子,用来入门吧,以后慢慢研究,当然,等有用的时候。
当然,使用这个玩意需要去下载JAR包的,BeanUTils下载地址为点击打开链接,他需要另一个第三包,下载地址为点击打开链接,将他们导入到工程路径中
首先贴上JavaBean的代码
package com.bird.beanutils;
import java.util.Date;
/**
* @use 一个简单的Java Bean
* @author Bird
*
*/
public class Person {
public String name;
private String sex;
private String password;
public int age;
public Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.bird.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.junit.Test;
/**
* @use 使用Apache的BeanUtils操作JavaBean
* @author Bird
*
*/
@SuppressWarnings("unused")
public class Demo1 {
@Test
public void test1() throws Exception{
Person p = new Person();
BeanUtils.setProperty(p, "name", "Bird");//使用BeanUtils设置name的值
System.out.println(p.getName());
}
@Test
public void test2() throws Exception{//BeanUtils支持8种基本类型之间的转换
Person p = new Person();
String name = "Bird";
String sex = "man";
String age = "19";
String password = "123";//由表单提交过来的都是String类型
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "sex", sex);
BeanUtils.setProperty(p, "age", age);//虽然age是整形,但是BeanUtils可以转换赋值
BeanUtils.setProperty(p, "password", password);
System.out.println(p.getAge());
System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getSex());
}
@Test
public void test3() throws Exception{//注册BeanUtils的转换器,进行转换其他非8种基本类型的类型
String birthday = "1992-01-01";
ConvertUtils.register(new Converter(){//匿名内部类,实现Converter接口
@SuppressWarnings("unchecked")
public Object convert(Class type, Object value) {
if(value == null){//如果传入值为空,
return null;
}
if(!(value instanceof String)){//如果不是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, "birthday", birthday);
System.out.println(p.getBirthday());
}
@SuppressWarnings("unchecked")
@Test
public void test5() throws Exception{//对Map的操作
Map map = new HashMap();
map.put("age", "19");
Person bean = new Person();
BeanUtils.populate(bean, map);//用Map集合中的值填充Bean的值
System.out.println(bean.getAge());
}
}