NamedJDBCTemplate Parameters is list of lists

2019-06-03 14:38发布

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条回答
手持菜刀,她持情操
2楼-- · 2019-06-03 15:31

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.

查看更多
登录 后发表回答