简体   繁体   English

如何在JdbcTemplate中的List中使用IN()子句?

[英]How to use IN() clause with List in JdbcTemplate?

I'm trying to query with JdbcTemplate using a list to match: 我正在尝试使用列表匹配的JdbcTemplate进行查询:

List foos = jdbcTemplate.query("select * from foo where name IN (?)", 
    new Object[] { Arrays.asList("foo1", "foo2", "foo3")}, 
    new FooMapper()
);

Result: the database columns are not matched, even though the name column equal the string foo1 . 结果:即使name列等于字符串foo1 ,数据库列也不匹配。 Why? 为什么?

If possible with JdbcTemplate without named parameters. 如果可能,可以使用不带命名参数的JdbcTemplate

Set<String> names = ...;

MapSqlParameterSource fooParams = new MapSqlParameterSource();
fooParams.addValue("names", names);

List<Foo> foo = getJdbcTemplate().query("SELECT * FROM foo WHERE name IN (:names)",
     fooParams, getRowMapper());

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

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