したがって、これが、JDBCTemplateを使用してデータベースから自動生成されたID値を取得する最も効率的な方法であると私は信じるようになりました。
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps =
connection.prepareStatement(INSERT_SQL, new String[] {"ID_FIELD"});
// Configure the PreparedStatement HERE!
return ps;
}
},
keyHolder);
私の問題は、可変数の値を挿入することがよくあり(JDBCTemplate.update(String, Object[])
実際には正確に必要な値です)、PreparedStatement
一度に1つずつ挿入できるように見えることです(setString
など)。配列をループするのはとても...エレガントではないようです。