要开启属性可达不然会有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);
}