如何使用数组列表创建一个动态在其中查询子句
我有一个列表< string>类别和每个类别的类别,我想通过结合和操作员将它们添加到我的Where子句中,例如:
>
由于类别列表的大小正在发生变化,因此我不能做这样的事情:
String sql = "SELECT question_id FROM question WHERE category = ? AND category = ?";
jdbcTemplate.query(sql, stringMapper, "categ1", "categ2");
如何实现自己想要的?
I have a List<String> of categories and for each category, I want to add them to my WHERE clause by combining with AND operator like:SELECT question_id FROM question WHERE category = categ1 AND category = categ2 AND category = ...
Since the size of the categories list is changing, I cannot do something like this:
String sql = "SELECT question_id FROM question WHERE category = ? AND category = ?";
jdbcTemplate.query(sql, stringMapper, "categ1", "categ2");
How can I achieve what I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要么检查Spring句柄的JDBC模板是否使用语法为您使用,该语法可能是类似的(来自,我认为确实如此)
或写出可能出现的问题的查询:
注意:
stream()
,以生成“?”。如果您使用 commons-lang3 ,您可以通过”(“ + strignutils.repeat(“?”?'“,”,“,”,categories.size())替换它。 +“)”
(Javadoc中的示例可能是使用这种使用进行的)。1 = 1
以及和
。Either check if JDBC Template from Spring handle that for you using a syntax which could be something like (from the doc, I don't think it does)
Or write your own query with the problems that may arise:
Notes:
stream()
in a more or less strange fashion in order to generate the "?". If you use commons-lang3, you can replace it by"(" + StringUtils.repeat("?", ", ", categories.size()) + ")"
(the example in the javadoc was probably done with this kind of use).1=1
as well as theand
.我相信这可能对您有用:
I believe this may work for you: