NamedJDBCTemplate Parameters is list of lists

两盒软妹~` 提交于 2019-12-08 07:12:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!