首页>代码>ssm分页查询实例 ,只需一个查询就可以>/page/src/com/common/paginate/ReflectHelper.java
package com.common.paginate;

import java.lang.reflect.Field;

/**
 * 分页辅助类
 * 
 * @作者 bulang
 * @创建日期 2012-8-15下午08:59:50
 * @版本 V 1.0
 */
public class ReflectHelper {
	/**
	 * 获取obj对象fieldName的Field
	 * @param obj
	 * @param fieldName
	 * @return
	 */
	public static Field getFieldByFieldName(Object obj, String fieldName) {
		for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
				.getSuperclass()) {
			try {
				return superClass.getDeclaredField(fieldName);
			} catch (NoSuchFieldException e) {
			}
		}
		return null;
	}

	/**
	 * 获取obj对象fieldName的属性值
	 * @param obj
	 * @param fieldName
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static Object getValueByFieldName(Object obj, String fieldName)
			throws SecurityException, NoSuchFieldException,
			IllegalArgumentException, IllegalAccessException {
		Field field = getFieldByFieldName(obj, fieldName);
		Object value = null;
		if(field!=null){
			if (field.isAccessible()) {
				value = field.get(obj);
			} else {
				field.setAccessible(true);
				value = field.get(obj);
				field.setAccessible(false);
			}
		}
		return value;
	}

	/**
	 * 设置obj对象fieldName的属性值
	 * @param obj
	 * @param fieldName
	 * @param value
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static void setValueByFieldName(Object obj, String fieldName,
			Object value) throws SecurityException, NoSuchFieldException,
			IllegalArgumentException, IllegalAccessException {
		Field field = obj.getClass().getDeclaredField(fieldName);
		if (field.isAccessible()) {
			field.set(obj, value);
		} else {
			field.setAccessible(true);
			field.set(obj, value);
			field.setAccessible(false);
		}
	}
}
最近下载更多
13940562934  LV22 2023年9月27日
amour1  LV11 2021年12月3日
ewan007  LV30 2021年1月27日
a991206  LV10 2021年1月5日
桃花人  LV6 2020年12月17日
IT白夜  LV12 2020年12月17日
1900110734  LV6 2020年12月4日
240598911  LV10 2020年11月25日
怎么取名字啊14  LV4 2020年9月27日
hjh844615  LV6 2020年9月20日
最近浏览更多
f22m1a2b2  LV17 1月23日
zhoubowen  LV3 2024年12月29日
Ella0902 2024年12月18日
暂无贡献等级
xzg123456  LV6 2024年6月18日
h G24741  LV2 2024年3月12日
KIKO666 2024年2月21日
暂无贡献等级
沈从文  LV3 2024年1月1日
uni-code_0123  LV1 2023年11月28日
201711  LV1 2023年9月20日
 LV7 2023年8月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友