JdbcTemplate IN Clause for String elements

≯℡__Kan透↙ 提交于 2019-11-30 04:55:08

问题


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?


回答1:


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());


来源:https://stackoverflow.com/questions/8489138/jdbctemplate-in-clause-for-string-elements

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