Spring's JDBCTemplate provides a way to efficiently execute SQL queries, including queries using the IN() clause. Here's a common, but probably not optimal, approach:
<code class="language-java">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()); }</code>
A more elegant solution is to use parameter sources. This method includes:
<code class="language-java">Set<Integer> ids = ...; MapSqlParameterSource parameters = new MapSqlParameterSource(); parameters.addValue("ids", ids); List<Foo> foo = getJdbcTemplate().query("SELECT * FROM foo WHERE a IN (:ids)", parameters, getRowMapper());</code>
The above is the detailed content of How Can Spring's JDBCTemplate Optimize IN() Queries?. For more information, please follow other related articles on the PHP Chinese website!