0

所以我一直相信这是使用 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允许一次插入一个(setString等等)。遍历数组似乎是如此...不雅。

4

1 回答 1

0

好吧,既然这是风滚草,我猜没有其他方法可以做到这一点。我最终创建了一个类来处理这个问题,这样我就可以绕过对final的要求。

于 2011-06-29T19:47:30.573 回答