Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I ask JDBCTemplate to expand a list parameter for use in an in() clause? [duplicate]

Can I do something like this:

select * from mytable m where m.group_id in (?)

... and pass in a list or array of arguments to be expanded in to my parameter, ie:

select * from mytable m where m.group_id in (1,2,3,4)

Specifically, I'm using Spring and the JdbcTemplate/SimpleJdbcTemplate classes.

like image 480
royal Avatar asked Aug 31 '10 23:08

royal


1 Answers

You can do it by using NamedParameterJdbcTemplate.

With your sample it would go something like:

NamedParameterJdbcTemplate db = ...;
List paramList = ...;

Map idsMap = Collections.singletonMap("ids", paramList);
db.query("select * from mytable m where m.group_id in (:ids)", idsMap);
like image 86
kaarlo Avatar answered Oct 14 '22 18:10

kaarlo