Effective IN() SQL Queries with Spring's JDBCTemplate
You have an IN() query that currently has you building a clause with multiple lines of code:
StringBuilder jobTypeInClauseBuilder = new StringBuilder(); for(int i = 0; i < jobTypes.length; i ) { Type jobType = jobTypes[i]; if(i != 0) { jobTypeInClauseBuilder.append(','); } jobTypeInClauseBuilder.append(jobType.convert()); }
You seek a more elegant way that is similar to prepared statements' parameter substitution.
Using a Parameter Source
To achieve your desired elegance, you can use a parameter source:
Setids = ...; MapSqlParameterSource parameters = new MapSqlParameterSource(); parameters.addValue("ids", ids); List foo = getJdbcTemplate().query("SELECT * FROM foo WHERE a IN (:ids)", parameters, getRowMapper());
NamedParameterJdbcTemplate
Note that this only works if getJdbcTemplate() returns an instance of type NamedParameterJdbcTemplate.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3