Spring SimpleJdbcInsert 与 JdbcTemplate

标签 spring performance spring-jdbc jdbctemplate simplejdbcinsert

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

使用 update() 也可以这样做JdbcTemplate的方法通过设置 PreparedStatement而不是参数映射。

我只想知道 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.



如果你打算使用 A SimpleJdbcInsert,那么还有 实际插入正在使用 JdbcTemplate 处理 .所以绝对在性能方面 SimpleJdbcInsert 不能比 JdbcTemplate 更好。

因此,在性能方面,使用 SimpleJdbcInsert 不会有好处。

但是执行插入操作到使用 SimpleJdbcInsert 的多个表肯定比 JdbcTemplate 类具有更好的功能 .在某些情况下,您可能希望在大量表中插入数据,并且您可能希望减少编码。在这些情况下,使用 SimpleJdbcInsert 可能是一个非常好的选择。请参阅此 example明白这一点。

关于Spring SimpleJdbcInsert 与 JdbcTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32537570/

相关文章:

java - hibernate4-maven-plugin 找不到带注释的类

java - apache shiro saltedauthentication.hashProvidedCredentials 遇到问题,未给出预期的哈希值

java - 回滚事务功能是否仅在抛出异常时才起作用?

java - Spring 安全: Expecting to only find a single bean for type interface org. springframework.security.authentication.AuthenticationManager

Spring JDBC RowMapper 用于急切获取

sql - 更新表中所有行的有效方法

jQuery 什么更快 : selectors or methods?

c# - 我可以加快此查询以检索域中的所有计算机吗?

java - PSQL异常 : This connection has been closed - Spring Boot + AWS RDS + Postgres

java - 无法在 Spring Boot 中使用 JDBC 身份验证创建安全连接