使用beanUtils操作javabean

有一个Person类

import java.util.Date;

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.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.text.ParseException;
import java.text.SimpleDateFormat;

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;
public class Demo1 {
	
	@Test
	public void test1() throws IllegalAccessException, InvocationTargetException{
		
		Person p = new Person();
		BeanUtils.setProperty(p, "name", "小明");
		System.out.println(p.getName());
	}
	
	@Test
	public void test2() throws IllegalAccessException, InvocationTargetException{
		//假设有表单传来数据
		String name = "zhangsan";
		String password = "password";
		String age = "24";
		
		Person p = new Person();
		BeanUtils.setProperty(p, "name", name);
		BeanUtils.setProperty(p, "password", password);
		BeanUtils.setProperty(p, "age", age);
		
		//自动进行了数据类型的转化,只支持8种基本类型
		System.out.println(p.getName());
		System.out.println(p.getPassword());
		System.out.println(p.getAge());

	}
	
	//以下是编写时间的转化器
	@Test
	public void test3() throws IllegalAccessException, InvocationTargetException{
		String birthday = "1990-03-16";
		
		//让日期赋到bean的日期属性上,需注册转换器
		ConvertUtils.register(new Converter(){
			public Object convert(Class type, Object value) {
				if(value==null){
					return null;
				}
				if(!(value instanceof String)){
					throw new  ConversionException("only support the type of String");
				}
				
				String str = (String) value;
				if(str.trim().equals("")){
					return null;
				}
				
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				try {
					return df.parse(str);
				} catch (ParseException e) {
					throw new RuntimeException(e); //异常链不能断
				}
				
			}
		}, Date.class);
		Person p = new Person();
		BeanUtils.setProperty(p,"birthday",birthday);
		//转成时间显示格式
		System.out.println(p.getBirthday().toLocaleString());
	}
	
	//使用Apache给的时间转换器,当你把时间赋值空时有异常
	@Test
	public void test4() throws IllegalAccessException, InvocationTargetException{
		String birthday = "1990-03-16";
		
		ConvertUtils.register(new DateLocaleConverter(),Date.class);
		
		Person p = new Person();
		BeanUtils.setProperty(p,"birthday",birthday);
		System.out.println(p.getBirthday());
	}
	
	//map集合的操作
	@Test
	public void test5() throws IllegalAccessException, InvocationTargetException{
		
		Map map = new HashMap();
		map.put("name", "zhangsan");
		map.put("password", "123");
		map.put("age", 24);
		map.put("birthday", "1990-04-17");
		
		ConvertUtils.register(new DateLocaleConverter(),Date.class);
		Person bean = new Person();
		
		//填充进去
		BeanUtils.populate(bean, map);
		
		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
发出的红包

打赏作者

久梦歌行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值