如何将 in-params 设置为 jdbcTemplate 查询函数?
How to set in-params with their occurrences to jdbcTemplate query function?
我试图执行一个大的 SQL 查询,它可以包含几个相同的 IN 参数,例如,{arg1, arg2, arg1, arg1, arg2}。使用 jdbcTemplate 我可以执行如下查询:
jdbcTemaplate.query(sqlQuery, new Object[] {arg1, arg2, arg1, arg1, arg2}, rowMap);
但在某些查询中,参数的数量可能很大,因此,编写类似 {arg, arg1, arg1, arg2, arg1, arg1, arg1, ...} 的参数看起来很糟糕。有没有办法为每个 in-param 定义一组出现索引并将其发送到 jdbcTemplate 的查询函数?例如,
ParamsOccurrences[] paramsOccurrences = {
new ParamsOccurrences(arg1, new int[] { 1, 4, 5, 6 }),
new ParamsOccurrences(arg2, new int[] { 2, 3 }) };
jdbcTemplate.query(sqlQuery, paramsOccurrences, rowMap);
我使用 Oracle 数据库。
我找到了适合我的解决方案。简单的方法是在jdbcTemplate的查询函数中使用PreparedStatementSetter参数。示例:
jdbcTemplate.query(sqlQuery, new PreparedStatementSetter() {
public void setValues(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setObject(1, arg1); //example for arg1
preparedStatement.setObject(2, arg1);
... //inside this method I just process every argument by setting specific numbers of occurrences in cycle
}
}, rowMap);
我试图执行一个大的 SQL 查询,它可以包含几个相同的 IN 参数,例如,{arg1, arg2, arg1, arg1, arg2}。使用 jdbcTemplate 我可以执行如下查询:
jdbcTemaplate.query(sqlQuery, new Object[] {arg1, arg2, arg1, arg1, arg2}, rowMap);
但在某些查询中,参数的数量可能很大,因此,编写类似 {arg, arg1, arg1, arg2, arg1, arg1, arg1, ...} 的参数看起来很糟糕。有没有办法为每个 in-param 定义一组出现索引并将其发送到 jdbcTemplate 的查询函数?例如,
ParamsOccurrences[] paramsOccurrences = {
new ParamsOccurrences(arg1, new int[] { 1, 4, 5, 6 }),
new ParamsOccurrences(arg2, new int[] { 2, 3 }) };
jdbcTemplate.query(sqlQuery, paramsOccurrences, rowMap);
我使用 Oracle 数据库。
我找到了适合我的解决方案。简单的方法是在jdbcTemplate的查询函数中使用PreparedStatementSetter参数。示例:
jdbcTemplate.query(sqlQuery, new PreparedStatementSetter() {
public void setValues(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setObject(1, arg1); //example for arg1
preparedStatement.setObject(2, arg1);
... //inside this method I just process every argument by setting specific numbers of occurrences in cycle
}
}, rowMap);