BeanUtil使用范例
package ttt;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BasicDynaClass;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.BigIntegerConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.ShortConverter;
import org.apache.commons.beanutils.converters.SqlDateConverter;
import org.apache.commons.beanutils.converters.SqlTimeConverter;
import org.apache.commons.beanutils.converters.SqlTimestampConverter;
public class Test {
public static void main(String[] args) throws ClassNotFoundException,
IllegalAccessException, InstantiationException,
InvocationTargetException, NoSuchMethodException {
/********************************* 转换值开始 ************/
ConvertUtils.deregister();
ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
ConvertUtils.register(new DoubleConverter(null), Double.class);
ConvertUtils.register(new FloatConverter(null), Float.class);
ConvertUtils.register(new SqlDateConverter(null), java.sql.Date.class);
ConvertUtils.register(new SqlTimeConverter(null), Time.class);
ConvertUtils.register(new SqlTimestampConverter(null), Timestamp.class);
ConvertUtils.register(new LongConverter(null), Long.class);
ConvertUtils.register(new ShortConverter(null), Short.class);
System.out.println(ConvertUtils.convert("abc", Integer.class)); // 0
ConvertUtils.register(new IntegerConverter(null), Integer.class);
System.out.println(ConvertUtils.convert("abc", Integer.class)); // null
/********************************** 转换值结束***********/
/************************************* 动态开始 *********************************/
// 定义动态属性
DynaProperty[] props = new DynaProperty[] {
new DynaProperty("username", String.class),
new DynaProperty("address", java.util.Map.class) };
// 动态类
BasicDynaClass dynaClass = new BasicDynaClass("person", null, props);
// 动态bean
DynaBean person = dynaClass.newInstance();
person.set("username", "jhlishero");// 设置值
Map<String, String> maps = new HashMap<String, String>();
maps.put("key1", "value1");
maps.put("key2", "value2");
person.set("address", maps);// 设置值
person.set("address", "key3", "value3");// 第二种方法设置map中的值
System.err.println(person.get("username"));// 获取字符串值
System.err.println(person.get("address", "key1"));// 获取map中值
System.err.println(person.get("address", "key3"));
// 使用PropertyUtils工具获取属性值
System.out.println(PropertyUtils.getSimpleProperty(person, "username"));
//使用BeanUtils取值
BeanUtils.setProperty(person, "username", "nihao");
System.out.println(BeanUtils.getProperty(person, "username"));
System.out.println("---"
+ BeanUtils.getMappedProperty(person, "address", "key1"));
/************************************* 动态结束 *********************************/
//读取类的所有属性
Student stu = new Student();
BeanUtils.setProperty(stu, "name", "nihao");
BeanUtils.getProperty(stu, "name");
PropertyUtils.getProperty(stu, "name");
PropertyDescriptor[] prop = PropertyUtils.getPropertyDescriptors(stu);
for (int i = 0; i < prop.length; ++i) {
System.out.println(prop[i].getName() + "+++++" + prop[i].getPropertyType());
System.out.println("--"+PropertyUtils.getSimpleProperty(stu,prop[i].getName()));
}
}
}