From 14ce04027d5427a3ed2dd7bb7ece338035456c35 Mon Sep 17 00:00:00 2001 From: mxd <838425805@qq.com> Date: Sun, 13 Sep 2020 15:13:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../magicapi/functions/DatabaseQuery.java | 106 +++++++++--------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/src/main/java/org/ssssssss/magicapi/functions/DatabaseQuery.java b/src/main/java/org/ssssssss/magicapi/functions/DatabaseQuery.java index 409d5a21..2fc3934f 100644 --- a/src/main/java/org/ssssssss/magicapi/functions/DatabaseQuery.java +++ b/src/main/java/org/ssssssss/magicapi/functions/DatabaseQuery.java @@ -220,69 +220,69 @@ public int update(String sql) { return value; } - public int save(String tableName,Map params){ - return save(tableName,params,"id"); - } +// public int save(String tableName,Map params){ +// return save(tableName,params,"id"); +// } /** * 如果已存在就修改,否则增加 */ - public int save(String tableName,Map data,String primaryKey){ +// public int save(String tableName,Map data,String primaryKey){ // Object[] params = new Object[]{data.get(primaryKey)}; // Integer count = dataSourceNode.getJdbcTemplate().queryForObject("select count(1) from "+tableName+" where "+primaryKey+" = ?", params, Integer.class); // if(count > 0){ // return jdbcUpdate(tableName,data,primaryKey); // } // return 0; - Object primaryKeyValue = data.get(primaryKey); - if(null == primaryKeyValue){ - return jdbcInsert(tableName,data,primaryKey); - } - return jdbcUpdate(tableName,data,primaryKey); - } - - public int jdbcUpdate(String tableName,Map data,String primaryKey){ - StringBuffer sb = new StringBuffer(); - sb.append("update "); - sb.append(tableName); - sb.append(" set "); - List params = new ArrayList<>(); - for(Map.Entry entry : data.entrySet()){ - String key = entry.getKey(); - if(!key.equals(primaryKey)){ - sb.append(key + "=" + "?,"); - params.add(entry.getValue()); - } - } - sb.append(" where "); - sb.append(primaryKey); - sb.append("=?"); - params.add(data.get(primaryKey)); - return dataSourceNode.getJdbcTemplate().update(sb.toString().replace("?, ","? "),params.toArray()); - } - - public int jdbcInsert(String tableName,Map data,String primaryKey){ - List params = new ArrayList<>(); - params.add(""); - List fields = new ArrayList<>(); - List valuePlaceholders = new ArrayList<>(); - StringBuffer sb = new StringBuffer(); - sb.append("insert into "); - sb.append(tableName); - for(Map.Entry entry : data.entrySet()){ - String key = entry.getKey(); - if(!key.equals(primaryKey)){ - fields.add(key); - valuePlaceholders.add("?"); - params.add(entry.getValue()); - } - } - sb.append("("+ primaryKey + "," + StringUtils.join(fields,",") +")"); - sb.append(" values(?,"+StringUtils.join(valuePlaceholders,",")+")"); - String id = UUID.randomUUID().toString().replace("-",""); - params.set(0,id); - return dataSourceNode.getJdbcTemplate().update(sb.toString(),params.toArray()); - } +// Object primaryKeyValue = data.get(primaryKey); +// if(null == primaryKeyValue){ +// return jdbcInsert(tableName,data,primaryKey); +// } +// return jdbcUpdate(tableName,data,primaryKey); +// } + +// public int jdbcUpdate(String tableName,Map data,String primaryKey){ +// StringBuffer sb = new StringBuffer(); +// sb.append("update "); +// sb.append(tableName); +// sb.append(" set "); +// List params = new ArrayList<>(); +// for(Map.Entry entry : data.entrySet()){ +// String key = entry.getKey(); +// if(!key.equals(primaryKey)){ +// sb.append(key + "=" + "?,"); +// params.add(entry.getValue()); +// } +// } +// sb.append(" where "); +// sb.append(primaryKey); +// sb.append("=?"); +// params.add(data.get(primaryKey)); +// return dataSourceNode.getJdbcTemplate().update(sb.toString().replace("?, ","? "),params.toArray()); +// } +// +// public int jdbcInsert(String tableName,Map data,String primaryKey){ +// List params = new ArrayList<>(); +// params.add(""); +// List fields = new ArrayList<>(); +// List valuePlaceholders = new ArrayList<>(); +// StringBuffer sb = new StringBuffer(); +// sb.append("insert into "); +// sb.append(tableName); +// for(Map.Entry entry : data.entrySet()){ +// String key = entry.getKey(); +// if(!key.equals(primaryKey)){ +// fields.add(key); +// valuePlaceholders.add("?"); +// params.add(entry.getValue()); +// } +// } +// sb.append("("+ primaryKey + "," + StringUtils.join(fields,",") +")"); +// sb.append(" values(?,"+StringUtils.join(valuePlaceholders,",")+")"); +// String id = UUID.randomUUID().toString().replace("-",""); +// params.set(0,id); +// return dataSourceNode.getJdbcTemplate().update(sb.toString(),params.toArray()); +// } /** * 插入并返回主键