Package gxa.bj.util;
Import java.lang.reflect.*;
public class Beanutil {
Query statements based on certain criteria
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 ();
}
Update the contents of other fields according to the primary key
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);//access to private value
Object value=f.get (obj);
Determine if the field has a primary key label
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;
}
Deletes data based on certain criteria, if the condition is a primary key and is only deleted by the primary key condition
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 statements for new data
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;
}
}
By reflection, the method of obtaining the SQL statement of database additions and deletions is obtained.