简体   繁体   English

如何做IN()和WHERE =? 使用Spring的JDBCTemplate进行SQL查询

[英]How to do IN() and WHERE=? SQL queries with Spring's JDBCTemplate

This is an extension of How to execute IN() SQL queries with Spring's JDBCTemplate effectivly? 这是如何有效地使用Spring的JDBCTemplate执行IN()SQL查询的扩展?

I wish to modify the query to be: 我希望将查询修改为:

"SELECT * FROM foo WHERE name=? and value IN (:ids)"

how can I modify the given code to support the name=? 如何修改给定的代码以支持name =? parameter 参数

Set<Integer> ids = ...;

MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("ids", ids);

List<Foo> foo = getJdbcTemplate().query("SELECT * FROM foo WHERE a IN (:ids)",
     getRowMapper(), parameters);

I was successful using both comments. 我成功地使用了这两条评论。

I will post the code below for those who may find this question later: 我将在下面发布以下代码,供以后查找此问题的人使用:

String sql = "SELECT * FROM foo WHERE name=:name and value IN (:ids)";

Set<Integer> ids = ...;
String name = "John";

MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("ids", ids);
parameters.addValue("name",name);

List<Foo> foo = getNamedParameterJdbcTemplate().query(sql, parameters, getRowMapper());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM