BeanUtil是appach为了方便bean使用而开发的,使用前,先要导入两个jar包。分别为commons-beanutil-1.9.2.jar和common-logging-1.2.jar。导入之后即可使用。
基本的应用:
package beanUtil;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.xml.crypto.Data;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;
public class demo1 {
@Test
public void test1() throws Exception {
Person p = new Person();
BeanUtils.setProperty(p, "name", "xxc");
System.out.println(BeanUtils.getProperty(p, "name"));
}
@Test
// 自动转化
public void test2() throws Exception {
String name = "lll";
String age = "23";
String password = "123";
String temp = "37.5";
// 此处只适用于八中基本数据类型的转换,若要进行复杂类型数据的转换,还需要另写转换器;
Person p = new Person();
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "temp", temp);
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getPassword());
System.out.println(p.getTemp());
}
@SuppressWarnings("deprecation")
@Test
// 此处测试转换器,转化复杂数据
public void test3() throws Exception {
String name = "lll";
String age = "23";
String password = "123";
String temp = "37.5";
String birthday = "1992-11-12";
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, "age", age);
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "temp", temp);
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getPassword());
System.out.println(p.getTemp());
System.out.println(p.getBirthday().toLocaleString());
}
@Test
public void test4() throws Exception {
Person p = new Person();
String name = "lll";
String age = "23";
String password = "123";
String temp = "37.5";
String birthday = "1992-11-12";
ConvertUtils.register(new DateLocaleConverter(), Date.class);
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "temp", temp);
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getPassword());
System.out.println(p.getTemp());
System.out.println(p.getBirthday().toLocaleString());
}
@Test
public void test5() throws Exception{
Person p = new Person();
String name = "lll";
String age = "23";
String password = "123";
String temp = "37.5";
String birthday = "1992-11-12";
Map map = new LinkedHashMap<String, String>();
map.put("name", name);
map.put("age", age);
map.put("password", password);
map.put("temp", temp);
map.put("birthday", birthday);
ConvertUtils.register(new DateLocaleConverter(), Date.class);
BeanUtils.populate(p, map);
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getPassword());
System.out.println(p.getTemp());
System.out.println(p.getBirthday().toLocaleString());
}
}