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

JAVA通过Map拼接SQL语句(InsertUpdate语句)package ;import ;import ls;import Utils;public class SQLUtils {

/** * 通过Map拼接Insert SQL语句 *

* @param tableName * @param dataMap * @return */ public static String genSqlInsert(String tableName, Map dataMap) { if(y(dataMap)) { return null; }

//⽣成INSERT INTO table(field1,field2) 部分 StringBuffer sbField = new StringBuffer(); //⽣成VALUES('value1','value2') 部分 StringBuffer sbValue = new StringBuffer();

("INSERT INTO " + rCase() + "("); for( entry : et()){ String mapKey = (); String mapValue = ue(); if((mapKey, )) { continue; }

("`" + mapKey + "`,"); ("'" + mapValue + "',"); }

sbField = LastChar(sbField); sbValue = LastChar(sbValue); return ng() + ") VALUES(" + ng() + ")"; }

/** * 通过Map拼接Update SQL语句 *

* @param tableName * @param operation * @param dataMap * @return */ public static String genSqlUpdate(String tableName, Map dataMap) { if(y(dataMap)) { return null; }

String idColumn = (); String idValue = (idColumn);

StringBuffer sb = new StringBuffer(); ("UPDATE "+ rCase() +" SET "); for( entry : et()){ String mapKey = (); String mapValue = ue(); if((mapKey, )) { continue; } if((rCase(), idColumn)) { continue; } ("`" + mapKey + "`='" + mapValue + "',"); }

sb = LastChar(sb);

return ("%s where %s='%s'", ng(), idColumn, idValue); }}

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

JAVA通过Map拼接SQL语句(InsertUpdate语句)package ;import ;import ls;import Utils;public class SQLUtils {

/** * 通过Map拼接Insert SQL语句 *

* @param tableName * @param dataMap * @return */ public static String genSqlInsert(String tableName, Map dataMap) { if(y(dataMap)) { return null; }

//⽣成INSERT INTO table(field1,field2) 部分 StringBuffer sbField = new StringBuffer(); //⽣成VALUES('value1','value2') 部分 StringBuffer sbValue = new StringBuffer();

("INSERT INTO " + rCase() + "("); for( entry : et()){ String mapKey = (); String mapValue = ue(); if((mapKey, )) { continue; }

("`" + mapKey + "`,"); ("'" + mapValue + "',"); }

sbField = LastChar(sbField); sbValue = LastChar(sbValue); return ng() + ") VALUES(" + ng() + ")"; }

/** * 通过Map拼接Update SQL语句 *

* @param tableName * @param operation * @param dataMap * @return */ public static String genSqlUpdate(String tableName, Map dataMap) { if(y(dataMap)) { return null; }

String idColumn = (); String idValue = (idColumn);

StringBuffer sb = new StringBuffer(); ("UPDATE "+ rCase() +" SET "); for( entry : et()){ String mapKey = (); String mapValue = ue(); if((mapKey, )) { continue; } if((rCase(), idColumn)) { continue; } ("`" + mapKey + "`='" + mapValue + "',"); }

sb = LastChar(sb);

return ("%s where %s='%s'", ng(), idColumn, idValue); }}