NamedJDBCTemplate参数是列表列表 [英] NamedJDBCTemplate Parameters is list of lists
问题描述
我的查询看起来像这样:
I have a query which looks something like this:
SELECT * FROM someTable t WHERE (t.a, t.b) IN (VALUES (1, 2), (3, 4))
它会选择任何记录 ta == 1 AND tb == 2
或 ta == 3 AND tb == 4
。
这似乎工作得很好。
但是,我无法想出一个干净的方法来将参数指定为 NamedJDBCTemplate
。我尝试给它一个列表列表(例如,列表< List< int>>
),但它似乎很难实现。
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)"))
(我实际上在Kotlin工作,但这不会对这个问题产生影响)
(I'm actually working in Kotlin, but that shouldn't have an effect on this question)
推荐答案
您可以将值作为对象数组的集合传递:
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})));
我们使用Google Guava实例化集合,因此我使用了 ImmutableMap
和列出
,但显然你可以按照自己的意愿创建它们。
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.
这篇关于NamedJDBCTemplate参数是列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!