public void insert(String table, Map fields) {
String sql = "";
try {
Assert.hasText(table, "表名不能为空");
Assert.notEmpty(fields, "字段不能为空");
table = quoteCol(table);
Object[] cols = fields.keySet().toArray();
Object[] values = new Object[cols.length];
for (int i = 0; i < cols.length; i++) {
if (fields.get(cols[i]) == null) {
values[i] = null;
} else {
values[i] = fields.get(cols[i]).toString();
}
cols[i] = quoteCol(cols[i].toString());
}
sql = "INSERT INTO " + table + " ("
+ StringUtil.implode(", ", cols) + ") VALUES ("
+ StringUtil.implodeValue(", ", values) + ")";
//System.out.println(sql);
// for(int i=0;i<values.length;i++){
// System.out.println(values[i]);
// }
jdbcTemplate.update(sql, values);
} catch (Exception e) {
// e.printStackTrace();
throw new DBRuntimeException(e, sql);
}
}
封装的insert 语句
最新推荐文章于 2021-04-02 22:58:26 发布