问题
I have a query which looks something like this:
SELECT * FROM someTable t WHERE (t.a, t.b) IN (VALUES (1, 2), (3, 4))
And it would select any records where t.a == 1 AND t.b == 2
or t.a == 3 AND t.b == 4
.
This seems to work just fine.
However, I can't figure out a clean way to specify the parameter to NamedJDBCTemplate
. I tried giving it a list of lists (i.e., List<List<int>>
), but it seems to blow up doing that.
val query = "SELECT * FROM someTable t WHERE (t.a, t.b) IN (VALUES :values)"
namedJdbcTemplate.queryForList(query, mapOf("values" to listOf(listOf(1, 2), listOf(3, 4))))
I also tried manually converting the value to a string, but that doesn't make it happy either.
namedJdbcTemplate.queryForList(query, mapOf("values" to "(1, 2), (3, 4)"))
(I'm actually working in Kotlin, but that shouldn't have an effect on this question)
回答1:
You can pass your values in as a collection of object arrays:
namedJdbcTemplate.queryForList(query, ImmutableMap.of(Lists.newArrayList(new Object[] {1,2}, new Object[]{3,4})));
We use Google Guava to instantiate collections hence why I've used ImmutableMap
and Lists
, but obviously you can go about creating them how you wish.
来源:https://stackoverflow.com/questions/50261515/namedjdbctemplate-parameters-is-list-of-lists