0

I'm trying to query with JdbcTemplate using a list to match:

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. Why?

If possible with JdbcTemplate without named parameters.

membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

1
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());
Optional
  • 4,387
  • 4
  • 27
  • 45
  • 1
    Is there a solution without having to use `NamedParameterJdbcTemplate` and `MapSqlParameterSource`? – membersound Jul 25 '19 at 14:27
  • Yes. List names = new ArrayList<>(); //add Map namesMap = Collections.singletonMap("names", names); and then pass namesMap in your query params @membersound – Optional Jul 25 '19 at 14:30
  • So but then I'd still have to use `NamedParameterJdbcTemplate`? I'm looking for a solution with `JdbcTemplate` directly, if possible. – membersound Jul 25 '19 at 14:38
  • No. It just replaces `new Object[] { Arrays.asList("foo1", "foo2", "foo3")},` in your sample with namesMap which is java map. – Optional Jul 25 '19 at 14:40
  • Well, `JdbcTemplate.query(sql, Collections.singletonMap(..), new FooMapper());`seems to be invalid. There is no query method in the normal JdbcTemplate that can take those args! – membersound Jul 25 '19 at 15:21