BeanUtil使用范例



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()));
		}

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值