2023年8月3日发(作者:)

Java通过Map拼接⽣成SQL语句import p;import or;import ;

public class SQLUtils { public static void main(String[] args) {

Map map = new HashMap<>(); ("code", "001"); ("name", "demo"); ("Key_code", "002"); try { n(getSql("table_name", "select", map, false, "")); } catch (Exception e) { tackTrace(); }

}

/** *

动态组装

简单sql语法 * * @param tableName

表名 * @param operation

操作标识符 select|delete|update ,默认为 select * @param mapData

数据的map集合 * @param useMySQL true|false , false

为使⽤动态组装SQL,true为使⽤⾃已的sql * @param mySql

⾃已的sql *

注意:update

这⾥,where xxx = xxx ,的时候,mapData

⾥的键必须要有 Key_

前缀(其他的

并不影响到) * @return * select: select a.* from demo a where ='001' and ='demo' * delete: delete from demo a where ='001' and ='demo' * insert: insert into demo (code,name,Key_code) values( '001','demo') * update: update demo a set ='001',='demo' where ='002' * @throws Exception */ public static String getSql(String tableName, String operation, Map mapData, boolean useMySQL, String mySql) throws Exception { String sql = null; //

使⽤组装sql的功能 if (!useMySQL) { if (!(tableName != null && !("") && () > 0)) { throw new Exception(" 参数 tableName 的值为空!"); } else if (!(mapData != null && !("") && () > 0)) { throw new Exception(" 参数 mapData 的值为空!"); } //

操作标识

默认为 select String operations = "select"; String condition = " a.* from " + tableName + " a where "; if (operation != null && !("")) { if (("update") || ("UPDATE")) { operations = "update"; condition = " " + tableName + " a set "; } else if (("delete") || ("DELETE")) { operations = "delete"; condition = " from " + tableName + " a where "; } else if (("insert") || ("INSERT")) { operations = "insert"; condition = " into " + tableName + " ("; String link = ""; Iterator iterator = ().iterator(); while (t()) { String next = (String) (); condition += link + next; link = ","; } } condition += ") values( "; } } String value = ""; String link = ""; String keyValueOperations = " where "; Iterator> iterator = et().iterator(); while (t()) { next = (); if (ue() instanceof String) { value = "'" + ue() + "'"; } else { value = "" + ue() + ""; } if (().toString().lastIndexOf("Key_") == -1) { if (!("insert")) { if (("select") || ("delete")) { condition += link + "a." + (); condition += "=" + value; link = " and "; } else { condition += link + "a." + (); condition += "=" + value; link = ","; } } else { condition += link + value; link = ","; } } else { continue; } }

//

组装 insert sql

的结尾 if (("insert")) { condition += ")"; } else if (("update")) { //

组装 update sql

的结尾 condition += " where "; String and = ""; Iterator> iterator1 = et().iterator(); while (t()) { next = (); if (ue() instanceof String) { value = "'" + ue() + "'"; } else { value = "" + ue() + ""; } String key = ().toString(); if (dexOf("Key_") != -1) { key = ing(f("Key_") + 4, ()); condition += and + "a." + key + "=" + value; and = " and "; } } }

sql = operations + condition; } else { //

不使⽤组装sql的功能 sql = mySql; } return sql; }}

2023年8月3日发(作者:)

Java通过Map拼接⽣成SQL语句import p;import or;import ;

public class SQLUtils { public static void main(String[] args) {

Map map = new HashMap<>(); ("code", "001"); ("name", "demo"); ("Key_code", "002"); try { n(getSql("table_name", "select", map, false, "")); } catch (Exception e) { tackTrace(); }

}

/** *

动态组装

简单sql语法 * * @param tableName

表名 * @param operation

操作标识符 select|delete|update ,默认为 select * @param mapData

数据的map集合 * @param useMySQL true|false , false

为使⽤动态组装SQL,true为使⽤⾃已的sql * @param mySql

⾃已的sql *

注意:update

这⾥,where xxx = xxx ,的时候,mapData

⾥的键必须要有 Key_

前缀(其他的

并不影响到) * @return * select: select a.* from demo a where ='001' and ='demo' * delete: delete from demo a where ='001' and ='demo' * insert: insert into demo (code,name,Key_code) values( '001','demo') * update: update demo a set ='001',='demo' where ='002' * @throws Exception */ public static String getSql(String tableName, String operation, Map mapData, boolean useMySQL, String mySql) throws Exception { String sql = null; //

使⽤组装sql的功能 if (!useMySQL) { if (!(tableName != null && !("") && () > 0)) { throw new Exception(" 参数 tableName 的值为空!"); } else if (!(mapData != null && !("") && () > 0)) { throw new Exception(" 参数 mapData 的值为空!"); } //

操作标识

默认为 select String operations = "select"; String condition = " a.* from " + tableName + " a where "; if (operation != null && !("")) { if (("update") || ("UPDATE")) { operations = "update"; condition = " " + tableName + " a set "; } else if (("delete") || ("DELETE")) { operations = "delete"; condition = " from " + tableName + " a where "; } else if (("insert") || ("INSERT")) { operations = "insert"; condition = " into " + tableName + " ("; String link = ""; Iterator iterator = ().iterator(); while (t()) { String next = (String) (); condition += link + next; link = ","; } } condition += ") values( "; } } String value = ""; String link = ""; String keyValueOperations = " where "; Iterator> iterator = et().iterator(); while (t()) { next = (); if (ue() instanceof String) { value = "'" + ue() + "'"; } else { value = "" + ue() + ""; } if (().toString().lastIndexOf("Key_") == -1) { if (!("insert")) { if (("select") || ("delete")) { condition += link + "a." + (); condition += "=" + value; link = " and "; } else { condition += link + "a." + (); condition += "=" + value; link = ","; } } else { condition += link + value; link = ","; } } else { continue; } }

//

组装 insert sql

的结尾 if (("insert")) { condition += ")"; } else if (("update")) { //

组装 update sql

的结尾 condition += " where "; String and = ""; Iterator> iterator1 = et().iterator(); while (t()) { next = (); if (ue() instanceof String) { value = "'" + ue() + "'"; } else { value = "" + ue() + ""; } String key = ().toString(); if (dexOf("Key_") != -1) { key = ing(f("Key_") + 4, ()); condition += and + "a." + key + "=" + value; and = " and "; } } }

sql = operations + condition; } else { //

不使⽤组装sql的功能 sql = mySql; } return sql; }}