Skip to content

Commit

Permalink
添加注释
Browse files Browse the repository at this point in the history
  • Loading branch information
javamxd committed Sep 13, 2020
1 parent 3a2344a commit 14ce040
Showing 1 changed file with 53 additions and 53 deletions.
106 changes: 53 additions & 53 deletions src/main/java/org/ssssssss/magicapi/functions/DatabaseQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,69 +220,69 @@ public int update(String sql) {
return value;
}

public int save(String tableName,Map<String,Object> params){
return save(tableName,params,"id");
}
// public int save(String tableName,Map<String,Object> params){
// return save(tableName,params,"id");
// }

/**
* 如果已存在就修改,否则增加
*/
public int save(String tableName,Map<String,Object> data,String primaryKey){
// public int save(String tableName,Map<String,Object> 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<String,Object> data,String primaryKey){
StringBuffer sb = new StringBuffer();
sb.append("update ");
sb.append(tableName);
sb.append(" set ");
List<Object> params = new ArrayList<>();
for(Map.Entry<String, Object> 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<String,Object> data,String primaryKey){
List<Object> params = new ArrayList<>();
params.add("");
List<String> fields = new ArrayList<>();
List<String> valuePlaceholders = new ArrayList<>();
StringBuffer sb = new StringBuffer();
sb.append("insert into ");
sb.append(tableName);
for(Map.Entry<String, Object> 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<String,Object> data,String primaryKey){
// StringBuffer sb = new StringBuffer();
// sb.append("update ");
// sb.append(tableName);
// sb.append(" set ");
// List<Object> params = new ArrayList<>();
// for(Map.Entry<String, Object> 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<String,Object> data,String primaryKey){
// List<Object> params = new ArrayList<>();
// params.add("");
// List<String> fields = new ArrayList<>();
// List<String> valuePlaceholders = new ArrayList<>();
// StringBuffer sb = new StringBuffer();
// sb.append("insert into ");
// sb.append(tableName);
// for(Map.Entry<String, Object> 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());
// }

/**
* 插入并返回主键
Expand Down

0 comments on commit 14ce040

Please sign in to comment.