使用 JdbcTemplate.update() 执行存储过程是一种好习惯吗?
Is it a good practice to use JdbcTemplate.update() to execute a stored proc?
目前我正在使用 JdbcTemplate.update()
方法来执行一个存储过程,它基本上更新并插入一些记录,它
return 什么都没有。
但是我不确定使用 update()
方法执行存储过程是否是一个好习惯。使用该方法有什么缺点吗?
还是我必须使用 execute()
方法?我试图避免实施 execute(),因为它需要在我的应用程序中进行大量代码更改。
代码:
jdbcTemplate.update("call test_stored_proc(? , ?)", new Long[] {
userId, statusId });
你可以使用任何你想要的。 execute
是一种通用的 API,它可以 运行 任何东西,并让您知道它 return 的结果类型(结果集或受影响的行数)。 update
是一个特殊版本,用于 return 影响行或根本没有影响的查询。
JdbcTemplate
将查询最终委托给 java.sql.Statement
.
executeUpdate(String sql):
Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.
execute(String sql):
Executes the given SQL statement, which may return multiple results.
如果您使用基本 Spring jdbc 支持,您应该使用 SimpleJdbcCall 来调用存储过程,因为它可以帮助您处理与这些类型的调用相关的元数据。参见 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html
但就我个人而言,我会在 Spring Data JPA 中使用 @Procedure 注释,这让事情变得容易多了。参见 http://docs.spring.io/spring-data/jpa/docs/1.10.4.RELEASE/reference/html/#jpa.stored-procedures
因此您的代码更改为在接口上调用方法,如下所示:
@Procedure("test_stored_proc")
public void callIt(long userId, statusId);
请注意,您必须在您的应用中设置 Spring JPARepository 支持才能正常工作。
目前我正在使用 JdbcTemplate.update()
方法来执行一个存储过程,它基本上更新并插入一些记录,它
return 什么都没有。
但是我不确定使用 update()
方法执行存储过程是否是一个好习惯。使用该方法有什么缺点吗?
还是我必须使用 execute()
方法?我试图避免实施 execute(),因为它需要在我的应用程序中进行大量代码更改。
代码:
jdbcTemplate.update("call test_stored_proc(? , ?)", new Long[] {
userId, statusId });
你可以使用任何你想要的。 execute
是一种通用的 API,它可以 运行 任何东西,并让您知道它 return 的结果类型(结果集或受影响的行数)。 update
是一个特殊版本,用于 return 影响行或根本没有影响的查询。
JdbcTemplate
将查询最终委托给 java.sql.Statement
.
executeUpdate(String sql): Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.
execute(String sql): Executes the given SQL statement, which may return multiple results.
如果您使用基本 Spring jdbc 支持,您应该使用 SimpleJdbcCall 来调用存储过程,因为它可以帮助您处理与这些类型的调用相关的元数据。参见 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html
但就我个人而言,我会在 Spring Data JPA 中使用 @Procedure 注释,这让事情变得容易多了。参见 http://docs.spring.io/spring-data/jpa/docs/1.10.4.RELEASE/reference/html/#jpa.stored-procedures
因此您的代码更改为在接口上调用方法,如下所示:
@Procedure("test_stored_proc")
public void callIt(long userId, statusId);
请注意,您必须在您的应用中设置 Spring JPARepository 支持才能正常工作。