如何在 JDBCTemplates 中使用 SELECT IN 子句?
这是我第一次使用 JDBCTemplates,我遇到了需要使用如下所示查询的情况:
SELECT * FROM table WHERE field IN (?)
我该如何做?我已经尝试传递列表/数组值,但这并没有成功,我得到了一个异常。我当前的代码如下所示:
Long id = getJdbcTemplate().queryForLong(query, new Object[]{fieldIds});
Spring 文档 状态除了生成所需数量的“?”之外,没有其他方法可以做到这一点占位符以匹配参数列表的大小。有解决方法吗?
This is my first experience with JDBCTemplates and I ran into a case where I need to use a query that looks like this:
SELECT * FROM table WHERE field IN (?)
How do I do that? I already tried passing a list/array value but that didn't do the trick, I get an exception. My current code looks like this:
Long id = getJdbcTemplate().queryForLong(query, new Object[]{fieldIds});
Spring Documentation states that there is no way of doing this besides generating the required number of "?" placeholders to match the size of the parameter List. Is there a workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有一个解决方法,使用 NamedParameterJdbcTemplate 而不是 SimpleJdbcDaoSupport,您可以在其中执行以下操作:
但是,这对于您可以在列表中传递的参数数量存在潜在的灾难性限制,具体取决于您所使用的数据库。
There is a workaround using NamedParameterJdbcTemplate instead of SimpleJdbcDaoSupport, where you can do something like this:
This, however, has a potentially catastrophic limitation regarding the number of parameters you can pass in the list which depends on the DB you are using.
我认为您不能将其作为单个“?”来完成。它与 Spring JDBC 模板无关,它是核心 SQL。
您必须根据需要为尽可能多的对象建立 (?, ?, ?) 。
I don't think you can do this as a single '?'. It's nothing to do with Spring JDBC templates, it's core SQL.
You'll have to build up a (?, ?, ?) for as many of them as you need.
对于长列表(例如 Oracle 有 1000 个项目的限制),您可以将其分成更多选择:
For long list (ex. Oracle has limitation for 1000 items) you can just separate it to more selects:
请尝试使用 MapSqlParameterSource 和 NamedParameterJdbcTemplate。
在查询
IN (:array)中
Please try with MapSqlParameterSource with NamedParameterJdbcTemplate.
In query
IN (:array)
有一种方法可以做到,尽管我认为这不是最正确的。但我把它留在这里以防它对某人有帮助。
这样 jdbcTemplate 将查询作为完整的字符串读取并正确执行。
There is a way to do it although I don't think it is the most correct. But I leave it here in case it helps someone.
in this way jdbcTemplate reads the query as a complete string and it executes correctly.