JdbcTemplate IN子句为String元素 [英] JdbcTemplate IN Clause for String elements
问题描述
使用 NamedParameterJdbcTemplate
为其中的子句元素和其中一个似乎 List< String>
。 JdbcTemplate
用 List< String>
有办法吗?
还有一些其他类似的问题,可能有帮助的答案:
如何使用Spring的JDBCTemplate有效地执行IN()SQL查询?
为了使这种风格的查询在我的结尾,我必须从纯旧的JDBCTemplate切换到 NamedParameterJdbcTemplate
。 p>
这里是一些示例代码:
String query =select * from table where columnName in(:listOfValues);
List< String> nameRecordIDs = new ArrayList< String>();
// ...
//向集合添加值,然后
// ...
映射namedParameters = Collections.singletonMap(listOfValues,nameRecordIDs);
namedparameterJdbcTemplate.query(query,namedParameters,new MyMapper());
Am using NamedParameterJdbcTemplate
for where Clause elements and one of them seems to be List<String>
. JdbcTemplate
replaces them ?,?,?...(list size) but for a IN clause with List<String>
it has to be '?','?'....
Is there a way around this?
There a few other similar questions out there which might have helpful answers for you:
How to execute IN() SQL queries with Spring's JDBCTemplate effectivly?
To make this style of query work on my end, I have to switch from plain old JDBCTemplate to NamedParameterJdbcTemplate
.
Here is some example code:
String query = "select * from table where columnName in (:listOfValues)";
List<String> nameRecordIDs = new ArrayList<String>();
// ...
// add values to collection, then
// ...
Map namedParameters = Collections.singletonMap("listOfValues", nameRecordIDs);
namedparameterJdbcTemplate.query(query, namedParameters,new MyMapper());
这篇关于JdbcTemplate IN子句为String元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!