使用beanutils操作bean的属性

由于用内省操作bean的属性比较麻烦,所以apach公司研究出了使用beanutils操作bean的属性。

bean:

import java.util.Date;

//属性由getXXX或setXXX决定!
public class Person {

	private String name;      
	private String password;
	private int age;
	private Date birthday;
	//上述只是四个字段
	
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}

import java.lang.reflect.InvocationTargetException;
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;


//使用beanutils操作bean的属性
public class Demo1 {

	@Test
	public void test1() throws IllegalAccessException, InvocationTargetException {
		
		Person p = new Person(); 
		
		BeanUtils.setProperty(p, "name", "aaa");
		
		System.out.println(p.getName());
		
	}
	
	@Test
	public void test2() throws IllegalAccessException, InvocationTargetException {
		
		Person p = new Person();
		
		String name  = "aaa";
		String password = "123";
		String age = "23";   //只会转8种基本数据类型
		String birthday = "1991-02-15";
		
		//为了让日期赋到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 str = (String) value;
				if(str.trim().equals("")) {
					return null;
				}
				
				SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
				try {
					return date.parse(str);
				} catch (ParseException e) {
					throw new RuntimeException(e);   //异常链不能断,调用者得知道出现了什么异常
				}
			}

		}, Date.class);
		
		BeanUtils.setProperty(p, "name", name);
		BeanUtils.setProperty(p, "password", password);
		BeanUtils.setProperty(p, "age", age);
		BeanUtils.setProperty(p, "birthday", birthday);
		
		System.out.println(p.getName());
		System.out.println(p.getPassword());
		System.out.println(p.getAge());
		System.out.println(p.getBirthday().toLocaleString());
		
	}
	
	@Test
	public void test3() throws IllegalAccessException, InvocationTargetException {
	
		Person p = new Person();
		
		String name  = "aaa";
		String password = "123";
		String age = "23";   //只会转8种基本数据类型
		String birthday = "1991-02-15";
		
		ConvertUtils.register(new DateLocaleConverter(), Date.class);
		
		BeanUtils.setProperty(p, "name", name);
		BeanUtils.setProperty(p, "password", password);
		BeanUtils.setProperty(p, "age", age);
		BeanUtils.setProperty(p, "birthday", birthday);
		
		System.out.println(p.getName());
		System.out.println(p.getPassword());
		System.out.println(p.getAge());
		System.out.println(p.getBirthday());
		
	}
	
	@Test
	public void test4() throws IllegalAccessException, InvocationTargetException {
		
		Map map = new HashMap();
		map.put("name", "aaa");       //map关键字的名称要和bean属性名一致
		map.put("password", "123");
		map.put("age", "23");
		map.put("birthday", "1990-01-01");
		
		Person bean = new Person();
		ConvertUtils.register(new DateLocaleConverter(), Date.class);
		BeanUtils.populate(bean, map);   //用map集合中的值,填充bean的属性
		
		System.out.println(bean.getName());
		System.out.println(bean.getPassword());
		System.out.println(bean.getAge());
		System.out.println(bean.getBirthday());
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值