java bean方法_一些操作JavaBean的方法

关于JavaBean的介绍就不说了,直接上代码

测试类

package com.home.test;

import org.apache.commons.beanutils.BeanUtils;

import org.apache.commons.beanutils.PropertyUtils;

import com.home.util.BeanUtil;

public class Main {

public static void main(String[] args) throws Exception {

Person person = new Person("小飞", 23);

String propertyName1 = "name";

String propertyName2 = "age";

// 这是属性链,birthday是Date类型的,Date中有setTime(long time)方法

String propertyName3 = "birthday.time";

// 1.以下使用PropertyDescriptor操作JavaBean

BeanUtil.setProperty(person, propertyName1, "小明");

System.out.println(BeanUtil.getProperty(person, propertyName1));

// 2.使用BeanInfo操作JavaBean,方法里面一样可以获取和设置属性值

System.out.println(BeanUtil.beanToMap(person));

// 3.使用apache BeanUtils操作JavaBean

BeanUtils.setProperty(person, propertyName1, "小红");

BeanUtils.setProperty(person, propertyName3, "1111112222");

System.out.println(BeanUtils.getProperty(person, propertyName1));

System.out.println(BeanUtils.getProperty(person, propertyName3));

// BeanUtils操作中参数是String类型的 返回结果也是String类型

System.out.println(BeanUtils.getProperty(person, propertyName3).getClass().getName());

// 4.使用apache PropertyUtils操作JavaBean

PropertyUtils.setProperty(person, propertyName1, "小高");

PropertyUtils.setProperty(person, propertyName2, 25);

PropertyUtils.setProperty(person, propertyName3, 1111111111);

System.out.println(PropertyUtils.getProperty(person, propertyName1));

System.out.println(PropertyUtils.getProperty(person, propertyName2));

System.out.println(PropertyUtils.getProperty(person, propertyName3));

// 操作的是什么类型就返回什么类型

System.out.println(PropertyUtils.getProperty(person, propertyName2).getClass().getName());

}

}

Person实体:

package com.home.test;

import java.util.Date;

public class Person {

private String name;

private int age;

private Date birthday = new Date();// 这里要创建对象!

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

public Person(String name, int age) {

super();

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public String toString() {

return this.name + " " + this.age + " " + this.birthday;

}

}

自己封装的操作JavaBean的工具类BeanUtil

package com.home.util;

import java.beans.BeanInfo;

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.util.HashMap;

import java.util.Map;

/**

*

* @description 关于JavaBean的一些操作方法

* @author fj

* @createTime 2016年6月9日

*/

public class BeanUtil {

/**

*

* @description 使用PropertyDescriptor获取JavaBean对象的指定属性的值

* @param obj

* JavaBean对象

* @param propertyName

* 属性名

* @return get方法返回的值

* @throws IntrospectionException

* @throws IllegalAccessException

* @throws InvocationTargetException

*/

public static Object getProperty(Object obj, String propertyName) throws IntrospectionException, IllegalAccessException, InvocationTargetException {

PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass());// 获取javaBean的属性描述

Method methodGet = pd.getReadMethod(); // 得到get方法

if (methodGet != null) {

return methodGet.invoke(obj); // 调用get方法

}

return null;

}

/**

*

* @description 使用PropertyDescriptor设置JavaBean对象的指定属性的值

* @param obj

* JavaBean对象

* @param propertyName

* 属性名

* @param value

* 需要设置的值

* @throws IntrospectionException

* @throws IllegalAccessException

* @throws InvocationTargetException

*/

public static void setProperty(Object obj, String propertyName, Object value)

throws IntrospectionException, IllegalAccessException, InvocationTargetException {

PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass());// 获取javaBean的属性描述

Method methodSet = pd.getWriteMethod(); // 得到set方法

if (methodSet != null) {

methodSet.invoke(obj, value);// 调用set方法

}

}

/**

*

* @description 把一个对象转换为map,key为字段,value为字段的值

* @param obj JavaBean对象

* @return map

*/

public static Map beanToMap(Object obj) {

Map result = new HashMap();

if (obj == null) {

return result;

}

try {

Class> clazz = obj.getClass();

BeanInfo info = Introspector.getBeanInfo(clazz);

for (PropertyDescriptor pd : info.getPropertyDescriptors()) {

String propertyName = pd.getName();

if (propertyName.equals("class")) {

continue;

}

Method methodGet = pd.getReadMethod();

if (methodGet != null) {

result.put(propertyName, methodGet.invoke(obj));

}

}

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值