使用“在...中"在spring-jdbc中 [英] Using "where in" in spring-jdbc

查看:54
本文介绍了使用“在...中"在spring-jdbc中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在SQL中使用"where ... in"来删除一堆元素,就像这样:

Is there a way to delete a bunch of elements using "where... in" in SQL, like so:

HashSet<String> idStrings = ...;
SimpleJdbcTemplate template = getTemplate();
template.update("DELETE FROM records WHERE idstring IN (?)", idStrings);

我试图使一些使用此方法的旧代码正常工作,但是每当我尝试运行它时,Oracle JDBC驱动程序都会引发异常:

I am trying to get some old code to work that uses this method, but whenever I try to run it the Oracle JDBC drivers throw an exception:

QL状态[99999];错误代码[17004];无效的列类型;嵌套的异常是java.sql.SQLException:无效的列类型

QL state [99999]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type

这是与ojdbc5 11.2.0.1.0和spring-jdbc 3.0.3一起使用的

This is with ojdbc5 11.2.0.1.0 and spring-jdbc 3.0.3

推荐答案

可以,但是每个ID需要一个占位符(?),并且每个ID必须单独绑定.

It's possible, but you need one placeholder (?) per ID, and each ID must be bound separately.

您还可以使用 NamedParameterJdbcTemplate ,其中

[...]还允许将值列表扩展为适当的数字 占位符.

[...] also allows for expanding a List of values to the appropriate number of placeholders.

请注意不要在ID集中放入太多值.例如,Oracle将其限制为1000.

Be careful not to put too many values in the set of IDs. Oracle limits them to 1000, for example.

这篇关于使用“在...中"在spring-jdbc中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆