简体   繁体   中英

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?

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=? 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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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