Spring SimpleJdbcInsert 与 JdbcTemplate

Spring SimpleJdbcInsert vs JdbcTemplate

我有一个要求,我必须在数据库中插入一行并取回密钥(身份)。我想到了为此使用 SimpleJdbcInsert 。我正在将 JdbcTemplate 的对象传递给我的 SimpleJdbcInsert 并执行方法 executeAndReturnKey().

通过设置 PreparedStatement 而不是参数映射,可以使用 JdbcTemplateupdate() 方法完成相同的操作。

我只想知道 JdbcTemplate 在性能方面是否更好,我应该在 SimpleJdbcInsert 上使用它吗?如果是这样,那么它的卓越性能的原因是什么?

注意:我插入的不是一批记录,而是一条记录。

谢谢

SimpleJdbcInsert 与 JdbcTemplate

来自docs.spring.io

JdbcTemplate is the classic Spring JDBC approach and the most popular. This "lowest level" approach and all others use a JdbcTemplate under the covers.

注意:所有其他人都在幕后使用 JdbcTemplate

SimpleJdbcInsert optimize database metadata to limit the amount of necessary configuration. This approach simplifies coding so that you only need to provide the name of the table or procedure and provide a map of parameters matching the column names. This only works if the database provides adequate metadata. If the database doesn’t provide this metadata, you will have to provide explicit configuration of the parameters.

如果您打算使用 SimpleJdbcInsert,那么 实际的插入也是使用 JdbcTemplate 处理的。因此,就性能而言,SimpleJdbcInsert 肯定不会比 JdbcTemplate 更好。

因此,在性能方面,您无法使用 SimpleJdbcInsert 获益。

但是通过使用 SimpleJdbcInsert 对 多个表执行插入操作肯定比 JdbcTemplate class 具有更好的能力。那里可能在某些情况下你想在很多表中插入数据并且你可能希望少做一些事情 coding.In 在这些情况下,使用 SimpleJdbcInsert 可能是一个很好的 option.See 这个 example明白了。