beanutils操作方法

beanutils是第三方jar包

(1)BeanUtils相关包

commons-beanutils-1.8.0.jar

commons-logging-1.2.jar

(2)源代码

Bean : Student.java

import java.util.Date;

//属性是含有getXXX()或setXXX()方法
public class Person { //javabean
	private String name;	//字段
	private String password;	//字段
	private int age;	//字段
	private Date 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;
	}
	
	public String getAb(){
		return null;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}	
}

测试方法

使用beanUtils操纵bean属性(第三方)

<span style="white-space:pre">	</span>//beanutils只支持基本数据类型转换
	@Test
	public void test2() throws IllegalAccessException, InvocationTargetException{
		String name = "LeBronJames";
		String password = "123";
		String age = "30";
		
		Person p = new Person();
		BeanUtils.setProperty(p, "name", name);
		BeanUtils.setProperty(p, "password", password);
		BeanUtils.setProperty(p, "age", age);
		
		
		System.out.println(p.getName());
		System.out.println(p.getPassword());
		System.out.println(p.getAge());
		
	}
测试自带转换器类

<span style="white-space:pre">	</span>@Test
	public void test4() throws IllegalAccessException, InvocationTargetException{
		String name = "LeBron James";
		String password = "123";
		String age = "30";
		String birthday = "1984-12-30";
		Person p = new Person();
		ConvertUtils.register(new DateLocaleConverter(), Date.class);
		
		
		BeanUtils.setProperty(p, "name", name);
		BeanUtils.setProperty(p, "password", password);
		BeanUtils.setProperty(p, "age", age);//只支持8种基本数据类型
		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());
		
	}
测试自定义转换器类

<span style="white-space:pre">	</span>@Test
	public void test3() throws IllegalAccessException, InvocationTargetException{
		String name = "LeBron James";
		String password = "123";
		String age = "30";
		String birthday = "1984-12-30";
		
		Person p = new Person();
		//为了让日期赋值到bean的birthday属性上,我们给beanutils注册一个日期转换器
		//导入commons-beanutils-1.8.0-beta-sources.jar
		ConvertUtils.register(new Converter() {
			
			@Override
			public Object convert(Class type, Object value) {
				
				//1.
				if(value == null){
					return null;
				}
				//2.
				if(!(value instanceof String)){
					throw new ConversionException("只支持String类型的转换!!");
				}
				//3.
				String str = (String)value;
				if(str.trim().equals("")){
					return null;
				}
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//格式按文档给出的固定格式<span style="font-family: Verdana, Arial, Helvetica, sans-serif;">yyyy-MM-dd</span>

				try {
					return df.parse(str);
				} catch (ParseException e) {
					//不要e.printStackTrace(),要通知上一层(抛出异常给上一层)
					throw new RuntimeException(e);	//异常链不能断 比如 e 是被捕捉到的异常
				}
			}
		}, Date.class);//转换器写好了
		
		
		BeanUtils.setProperty(p, "name", name);
		BeanUtils.setProperty(p, "password", password);
		BeanUtils.setProperty(p, "age", age);//只支持8种基本数据类型
		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());
		
	}

*尽量不要去写打印异常的语句给自己看,要抛出异常给代码的上一级

BeanUtils的setProperty(object,name,value)方法需要的参数分别是

Object=加载类的对象

Name=类属性的名称

Value=所赋的值;

(注)BeanUtils的getProperty(object,name)方法的返回值是String类型,所以可以直接输出;

ConvertUtils.register(new Converter()中,new Converter()就相当于重写了Converter类中方法









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值