beanUtils操作JavaBean

需要导入的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());
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值