Java反射获取属性值

要开启属性可达不然会有IllegalAccessException异常

/**
	 * 获取属性的值
	 * @param fields
	 * @return
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	public static String getFieldValue(Object obj, Field[] fields) {
		StringBuffer sf=new StringBuffer();
		for (int i = 0; i < fields.length; i++) {
			//获取属性值
			try {
				//开启反射获取私有属性值
				fields[i].setAccessible(true);
				sf.append(fields[i].get(obj));
				if(i<fields.length-1)
					sf.append(",");
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return sf.toString();
	}
	
	/**
	 * 拿到父类子类的值
	 * @param obj
	 * @return
	 */
	public static String fatherAndSonValue(Object obj) {
		if(obj==null) {
			return null;
		}
		//获取class文件
		Class classInfo=obj.getClass();
		Field[] sonFields = classInfo.getDeclaredFields();
		String str1=getFieldValue(obj,sonFields);
		
		Field[] fatherFields = classInfo.getSuperclass().getDeclaredFields();
		String str2=getFieldValue(obj,fatherFields);
		return str1+","+str2;
	}

文件
package com.lzy.common.utils;

import java.lang.reflect.Field;
import java.sql.Timestamp;

import org.apache.ibatis.jdbc.SQL;

import com.lzy.common.entity.TestEntity;

/**

  • 反射工具类
  • @author lenovo

*/
public class ReflectionUtils {

/**
 *拿到父类与子类的属性
 * @param obj
 * @return
 */
public static String fatherAndSonFeild(Object obj) {
	if(obj==null) {
		return null;
	}
	//获取class文件
	Class classInfo=obj.getClass();
	Field[] sonFields = classInfo.getDeclaredFields();
	String str1=getField(sonFields);
	
	Field[] fatherFields = classInfo.getSuperclass().getDeclaredFields();
	String str2=getField(fatherFields);
	return str1+","+str2;
}
/**
 * 功能:(获取类的属性,凭借sql)
 */

// public static String getInsertFields(Object obj) {
// if(obj==null) {
// return null;
// }
// //获取class文件
// Class classInfo=obj.getClass();
// //classInfo.getSuperclass()获取父类属性
// //获取当前类属性
// Field[] declaredFields = classInfo.getDeclaredFields();
// return getField(declaredFields);
// }
/**
* 获取属性类的属性
* @param declaredFields
* @return
*/
public static String getField(Field[]declaredFields) {
StringBuffer sf=new StringBuffer();
for (int i = 0; i < declaredFields.length; i++) {
sf.append(declaredFields[i].getName());
if(i<declaredFields.length-1)
sf.append(",");
}
return sf.toString();
}

/**
 * 获取属性的值
 * @param fields
 * @return
 * @throws IllegalAccessException 
 * @throws IllegalArgumentException 
 */
public static String getFieldValue(Object obj, Field[] fields) {
	StringBuffer sf=new StringBuffer();
	for (int i = 0; i < fields.length; i++) {
		//获取属性值
		try {
			//开启反射获取私有属性值
			fields[i].setAccessible(true);
			Object value=fields[i].get(obj);
			
			//如果为字符串加单引号
			boolean flag=false;
			if(value!=null&&value instanceof String || value instanceof Timestamp) {
				flag=true;
				}
			//sf.append(fields[i].get(obj));
			//如果为字符串加单引号
			if(flag) {
				sf.append("'");
				sf.append(value);
				sf.append("'");
			}else {
				sf.append(value);
			}
			
			//sf.append("'"+fields[i].get(obj)+"'");
			
			
			
			if(i<fields.length-1)sf.append(",");
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	return sf.toString();
}

/**
 * 拿到父类子类的值
 * @param obj
 * @return
 */
public static String fatherAndSonValue(Object obj) {
	if(obj==null) {
		return null;
	}
	//获取class文件
	Class classInfo=obj.getClass();
	Field[] sonFields = classInfo.getDeclaredFields();
	String str1=getFieldValue(obj,sonFields);
	
	Field[] fatherFields = classInfo.getSuperclass().getDeclaredFields();
	String str2=getFieldValue(obj,fatherFields);
	return str1+","+str2;
}





public static void main(String[] args) {
	TestEntity testEntity = new TestEntity();
	//testEntity.setUserName("hhh1");
	testEntity.setUsername("hhh1");
	testEntity.setPhone("18273787090");
	testEntity.setEmail("2935268135@qq.com");
	testEntity.setPassword("123456");
	testEntity.setCreated(DateUtils.getTimestamp());
	testEntity.setUpdated(DateUtils.getTimestamp());
	String insertFields = fatherAndSonFeild(testEntity);
	String value=fatherAndSonValue(testEntity);
	SQL sql = new SQL() {{
		INSERT_INTO("mb_user");
		VALUES(insertFields,value);}
	};
	//DateUtils.getTimestamp();
	//System.err.println(DateUtils.getTimestamp());
	System.out.println(sql.toString());
}
}
---------------------------------
package com.lzy.common.utils;

import java.lang.reflect.Field;
import java.sql.Timestamp;

import org.apache.ibatis.jdbc.SQL;

import com.lzy.common.entity.TestEntity;

/**

  • 反射工具类
  • @author lenovo

*/
public class ReflectionUtils {

/**
 *拿到父类与子类的属性
 * @param obj
 * @return
 */
public static String fatherAndSonFeild(Object obj) {
	if(obj==null) {
		return null;
	}
	//获取class文件
	Class classInfo=obj.getClass();
	Field[] sonFields = classInfo.getDeclaredFields();
	String str1=getField(sonFields);
	
	Field[] fatherFields = classInfo.getSuperclass().getDeclaredFields();
	String str2=getField(fatherFields);
	return str1+","+str2;
}
/**
 * 功能:(获取类的属性,凭借sql)
 */

// public static String getInsertFields(Object obj) {
// if(obj==null) {
// return null;
// }
// //获取class文件
// Class classInfo=obj.getClass();
// //classInfo.getSuperclass()获取父类属性
// //获取当前类属性
// Field[] declaredFields = classInfo.getDeclaredFields();
// return getField(declaredFields);
// }
/**
* 获取属性类的属性
* @param declaredFields
* @return
*/
public static String getField(Field[]declaredFields) {
StringBuffer sf=new StringBuffer();
for (int i = 0; i < declaredFields.length; i++) {
sf.append(declaredFields[i].getName());
if(i<declaredFields.length-1)
sf.append(",");
}
return sf.toString();
}

/**
 * 获取属性的值
 * @param fields
 * @return
 * @throws IllegalAccessException 
 * @throws IllegalArgumentException 
 */
public static String getFieldValue(Object obj, Field[] fields) {
	StringBuffer sf=new StringBuffer();
	for (int i = 0; i < fields.length; i++) {
		//获取属性值
		try {
			//开启反射获取私有属性值
			fields[i].setAccessible(true);
			Object value=fields[i].get(obj);
			
			//如果为字符串加单引号
			boolean flag=false;
			if(value!=null&&value instanceof String || value instanceof Timestamp) {
				flag=true;
				}
			//sf.append(fields[i].get(obj));
			//如果为字符串加单引号
			if(flag) {
				sf.append("'");
				sf.append(value);
				sf.append("'");
			}else {
				sf.append(value);
			}
			
			//sf.append("'"+fields[i].get(obj)+"'");
			
			
			
			if(i<fields.length-1)sf.append(",");
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	return sf.toString();
}

/**
 * 拿到父类子类的值
 * @param obj
 * @return
 */
public static String fatherAndSonValue(Object obj) {
	if(obj==null) {
		return null;
	}
	//获取class文件
	Class classInfo=obj.getClass();
	Field[] sonFields = classInfo.getDeclaredFields();
	String str1=getFieldValue(obj,sonFields);
	
	Field[] fatherFields = classInfo.getSuperclass().getDeclaredFields();
	String str2=getFieldValue(obj,fatherFields);
	return str1+","+str2;
}





public static void main(String[] args) {
	TestEntity testEntity = new TestEntity();
	//testEntity.setUserName("hhh1");
	testEntity.setUsername("hhh1");
	testEntity.setPhone("18273787090");
	testEntity.setEmail("2935268135@qq.com");
	testEntity.setPassword("123456");
	testEntity.setCreated(DateUtils.getTimestamp());
	testEntity.setUpdated(DateUtils.getTimestamp());
	String insertFields = fatherAndSonFeild(testEntity);
	String value=fatherAndSonValue(testEntity);
	SQL sql = new SQL() {{
		INSERT_INTO("mb_user");
		VALUES(insertFields,value);}
	};
	//DateUtils.getTimestamp();
	//System.err.println(DateUtils.getTimestamp());
	System.out.println(sql.toString());
}
}
-----------------------------------------
package com.lzy.common.mybatis;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;

public interface BaseDao {

@InsertProvider(type=BaseProvider.class,method="save")
public void save(@Param("obj") Object obj,@Param("table")String table);

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值