需要使用spring jdbctemplate批量更新来插入记录。插入时如果找到重复记录,则需要更新记录,否则插入。我怎么做? 以下是我的代码。
注意:未包含异常处理。
result = jdbcTemplate.batchUpdate(
"insert ignore xxx set yy = ?, zz = ? where aa = ?",
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setDouble(1, Double.parseDouble(new JSONObject(jsonArray.get(i).toString()).get("aa").toString()));
ps.setDouble(2, Double.parseDouble(new JSONObject(jsonArray.get(i).toString()).get("bb").toString()));
ps.setString(3, new JSONObject(jsonArray.get(i).toString()).get("cc").toString());
}
public int getBatchSize() {
return jsonArray.length();
}
} );
}
答案 0 :(得分:1)
您可以使用以下选项之一:
merge
sql命令。希望有所帮助
答案 1 :(得分:0)
需要使用spring jdbctemplate批量更新来插入记录。而 插入如果找到重复记录,则需要更新记录 插入其他
为此,您应该使用标准SQL-2003命令merge。
在MySQL中,此命令具有以下语法: