通过反射,获得数据库增删改查的sql语句的方法
package gxa.bj.util;
import java.lang.reflect.*;
public class BeanUtil {
// 根据某些条件查询语句
public static String getSelectSQL(Object obj) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
Class<? extends Object> classzz=obj.getClass();
String tableName=classzz.getSimpleName();
StringBuffer sbsql=new StringBuffer();
sbsql.append("select * from "+tableName+" ");
sbsql.append("where 1=1 ");
Field[] fs=classzz.getDeclaredFields();
for(Field f:fs){
String methodName="get"+f.getName().substring(0,1).toUpperCase()+f.getName().substring(1);
Method m=classzz.getDeclaredMethod(methodName);
Object o=m.invoke(obj);
if(o!=null){
if(o instanceof String){
sbsql.append(" and "+f.getName()+"='"+o+"'");
}
else{
sbsql.append(" and "+f.getName()+"="+o);
}
}
}
return sbsql.toString();
}
// 根据主键更新其他字段内容呢
public static String getUpdate(Object obj) throws IllegalArgumentException, IllegalAccessException{
Class<? extends Object> classzz=obj.getClass();
String sql="update "+classzz.getSimpleName()+" set ";
String sql2=" ";
String sql3=" where ";
Field[] fs=classzz.getDeclaredFields();
for(Field f:fs){
f.setAccessible(true);//可访问private值
Object value=f.get(obj);
//判断该字段是否有主键标签
if(f.isAnnotationPresent(PrimaryKey.class)){
if(value instanceof String){
sql3 += f.getName()+"='"+value+"'";
}else{
sql3 += f.getName()+"="+value+" ";
}
}else if(!f.isAnnotationPresent(NonField.class)){
if(value instanceof String){
sql2 += f.getName()+"='"+value+"' ";
}else{
sql2 += f.getName()+"="+value+", ";
}
}
}
sql2=sql2.substring(0,sql2.length()-2);
sql=sql+sql2+sql3;
return sql;
}
// 根据某些条件删除数据,如果该条件是主键就只按照主键条件删除
public static String getDelete(Object obj) throws IllegalArgumentException, IllegalAccessException{
Class<? extends Object> classzz=obj.getClass();
String sql="delete from "+classzz.getSimpleName()+" where 1=1 ";
String sql2="";
Field[] fs=classzz.getDeclaredFields();
for(Field f:fs){
f.setAccessible(true);
Object value=f.get(obj);
if(f.isAnnotationPresent(PrimaryKey.class)&&value!=null){
if(value instanceof String){
sql2=" and "+f.getName()+"='"+value+"'";
}else{
sql2=" and "+f.getName()+"="+value;
}break;
}else{
if(value!=null){
if(value instanceof String){
sql2 += " and "+f.getName()+"='"+value+"'";
}else{
sql2 +=" and "+f.getName()+"="+value;
}
}
}
}
sql=sql+sql2;
return sql;
}
// 新加数据的sql语句
public static String getInsert(Object obj) throws IllegalArgumentException, IllegalAccessException{
Class<? extends Object> classzz=obj.getClass();
String sql="insert into "+classzz.getSimpleName()+" (";
String sql1="";
String sql2=") values (";
String sql3="";
String sql4=")";
Field[] fs=classzz.getDeclaredFields();
for(Field f:fs){
f.setAccessible(true);
Object value=f.get(obj);
if(value!=null){
sql1 += f.getName()+",";
if(value instanceof String){
sql3 +="'"+value+"',";
}else{
sql3 += value+",";}
}
}
sql1=sql1.substring(0,sql1.length()-1);
sql3=sql3.substring(0,sql3.length()-1);
sql=sql+sql1+sql2+sql3+sql4;
return sql;
}
}