如何在JDBCTemplates中使用SELECT IN子句? [英] How to use SELECT IN clause in JDBCTemplates?
问题描述
这是我第一次使用JDBCTemplates,并且遇到了需要使用如下查询的情况:
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文档指出没有除了生成所需数量的?"之外,还可以这样做占位符以匹配参数List的大小.有解决方法吗?
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?
推荐答案
我不认为您可以将其作为单个?"来完成.与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.
这篇关于如何在JDBCTemplates中使用SELECT IN子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!