使用 Spring 的 JDBC 支持获取输出参数

发布于 2024-08-01 17:41:29 字数 480 浏览 5 评论 0原文

我正在使用 Spring 的 JDBC 支持在 Oracle 数据库上运行 SQL 查询和更新。 我想插入一行,然后获取为其分配的键(使用 Oracle 序列)。 在正常的 JDBC 代码中,我将包含一个 RETURNING INTO 子句,然后注册一个输出参数 (这里有详细描述

但是,我只想使用 Spring 来处理我所有的 JDBC 工作。 对于非插入 SQL 语句,我当前正在使用 MapSqlParameterSource 对象并注册所有输入参数。 我也可以注册这样的输出参数并将其返回给我吗? 我查看了 Spring 文档的第 11 章部分,发现如果我使用存储过程,则支持输出参数,但如果可能的话,我想避免这样做。 谢谢。

I'm using Spring's JDBC support to run SQL queries and updates on an Oracle database. I'd like to insert a row and then get the key that it was assigned (using an Oracle sequence). In normal JDBC code, I would include a RETURNING INTO clause and then register an output parameter (well described here)

However, I would like to just use Spring to handle all my JDBC work for me. For non-insert SQL statements, I'm currently using a MapSqlParameterSource object and registering all my input parameters. Can I also register an output parameter like this and have it returned to me? I looked over the Chapter 11 portion of the Spring docs, and I saw there was support for an output parameter if I'm using stored procedures, but I would like to avoid doing that if possible. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷离° 2024-08-08 17:41:29

查看此代码 - 它可能会执行您想要的操作,但不确定 Oracle 是否支持此语法。

Check out this code - it may do what you want, not sure if Oracle supports this syntax though.

getReturnedGeneratedKeys() Spring JDBC Excample

遗心遗梦遗幸福 2024-08-08 17:41:29

我不认为 Spring JDBC 支持 API 为 OUT 参数提供显式支持,因此您可能需要退一步并使用 JdbcTemplate 提供的更通用的查询 API:

Object execute(PreparedStatementCreator psc, PreparedStatementCallback action)

这使您可以执行任意 JDBC Spring 管理的连接和 PrepatedStatement 范围内的操作。 缺点是像 ResultSets 这样的东西的处理和整理会成为你的问题。

I don't think the Spring JDBC support API provides explicit support for OUT parameters, so you may need to step back a bit and use the more general query API provided by JdbcTemplate:

Object execute(PreparedStatementCreator psc, PreparedStatementCallback action)

This lets you perform arbitrary JDBC operations within the scope of a Spring-managed connection and PrepatedStatement. The downside is that the handling and tidyup of things like ResultSets becomes your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文