您的位置:首页 > 编程语言 > Java开发

BeanUtils\PropertyUtils工具包操作JavaBean

2014-05-28 10:37 495 查看
BeanUtils

package com.in;

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

public class BeanUtilsDeo {
private Date birthDay = new Date();
private int x;

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public Date getBirthDay() {
return birthDay;
}

public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}

public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
BeanUtilsDeo bu = new BeanUtilsDeo();
//设置单个属性
String x = BeanUtils.getProperty(bu, "x");
System.out.println(x);

BeanUtils.setProperty(bu, "x", "2");
System.out.println(bu.getX());

//设置级联属性
BeanUtils.setProperty(bu, "birthDay.time", "2012-10-21");
System.out.println(bu.getBirthDay().getTime());

//设置map的属性
Map map = new HashMap();
map.put("name", "s");
BeanUtils.setProperty(map, "name", "yy");
System.out.println(map.get("name"));

//设置单个属性
PropertyUtils.setProperty(bu, "x", 9);
System.out.println(bu.getX());

PropertyUtils.setProperty(bu, "birthDay", new Date());
System.out.println(bu.getBirthDay());

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: