需要导入的jar包
package cn.zen.beanUtils;
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.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;
import cn.zen.entity.Person;
public class Demo1 {
@Test
public void method1() throws Exception {
Class clazz = Class.forName("cn.zen.entity.Person");
Person p = (Person) clazz.newInstance();
BeanUtils.setProperty(p, "_Name", "xiaopianzi");
System.out.println(p.get_Name());
}
@Test
public void method2() throws Exception {
String _name = "xiaopianzi";
String _pwd = "xiaopianzihaha";
int _age = 23;
Class clazz = Class.forName("cn.zen.entity.Person");
Person p = (Person) clazz.newInstance();
BeanUtils.setProperty(p, "_Name", _name);
BeanUtils.setProperty(p, "_Password", _pwd);
BeanUtils.setProperty(p, "_Age", _age);
System.out.println(p.get_Name());
System.out.println(p.get_Password());
System.out.println(p.get_Age());
}
@Test
public void method3() throws Exception {
String _name = "xiaopianzi";
String _pwd = "xiaopianzihaha";
int _age = 23;
String _birthday = "1990-11-18";
// 为了让日期赋到bean的_Birthday属性上,提前给BeanUtils注册转换器
ConvertUtils.register(new Converter() {
public Object convert(Class type, Object value) {
if (value == null) {
return null;
}
if (!(value instanceof String)) {
throw new ConversionException("只支持String类型的转换!");
}
String strVal = value.toString();
if (strVal.trim().equals("") || "".equals(strVal.trim())) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(strVal.trim());
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}, Date.class);
Class clazz = Class.forName("cn.zen.entity.Person");
Person p = (Person) clazz.newInstance();
BeanUtils.setProperty(p, "_Name", _name);
BeanUtils.setProperty(p, "_Password", _pwd);
BeanUtils.setProperty(p, "_Age", _age);
BeanUtils.setProperty(p, "_Birthday", _birthday);
System.out.println(p.get_Name());
System.out.println(p.get_Password());
System.out.println(p.get_Age());
System.out.println(p.get_Birthday());
}
@Test
public void method4() throws Exception {
String _name = "xiaopianzi";
String _pwd = "xiaopianzihaha";
int _age = 23;
String _birthday = "1990-11-18";
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Class clazz = Class.forName("cn.zen.entity.Person");
Person p = (Person) clazz.newInstance();
BeanUtils.setProperty(p, "_Name", _name);
BeanUtils.setProperty(p, "_Password", _pwd);
BeanUtils.setProperty(p, "_Age", _age);
BeanUtils.setProperty(p, "_Birthday", _birthday);
System.out.println(p.get_Name());
System.out.println(p.get_Password());
System.out.println(p.get_Age());
System.out.println(p.get_Birthday());
}
@Test
public void method5() throws Exception {
Map map = new HashMap();
map.put("_Name", "wangbaer");// key值为 bean的属性名称要一致;
map.put("_Password", "wangbaer123");
map.put("_Age", 23);
map.put("_Birthday", "1990-11-18");
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Class clazz = Class.forName("cn.zen.entity.Person");
Person p = (Person) clazz.newInstance();
BeanUtils.populate(p, map);//用map集合中的值,填充bean属性;
System.out.println(p.get_Name());
System.out.println(p.get_Password());
System.out.println(p.get_Age());
System.out.println(p.get_Birthday());
}
}