JdbcTemplate IN子句为String元素 [英] JdbcTemplate IN Clause for String elements

查看:381
本文介绍了JdbcTemplate IN子句为String元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆