leo*_*leo 5 java spring jdbctemplate
我必须对我将经常执行的数据库执行选择查询.它通过正常的声明完全正常:
List<Invoice> l = this.jdbcTemplate.query(
"SELECT id, name, stuff FROM example WHERE amount > ?",
new Object[] { "100" }, new RowMapper<Invoice>() {...} );
Run Code Online (Sandbox Code Playgroud)
我经常执行上述语句,因此出于性能原因,我想使用预准备语句.但是我现在不确定我是如何使用spring API来实现这一目的的.
我很困惑为什么Spring希望我给出一个PreparedStatementCreator查询参数的实例?我认为,每次使用查询方法时,我都不会创建新的预处理语句.因此,我认为下面代码片段的内容绝对没有意义,因为每次调用query-method时都会新创建预处理语句:
List<Invoice> l = this.jdbcTemplate.query(
new PreparedStatementCreator() {
String query = "SELECT id, name, stuff FROM example WHERE amount > ?";
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
return connection.prepareStatement(query);
}
},
new PreparedStatementSetter() {...},
new RowMapper<Invoice>() {...} );
Run Code Online (Sandbox Code Playgroud)
我必须创建自己的ReusablePreparedStatementCreator吗?这只会在第一次调用时创建一个新方法createPreparedStatement.
能否PreparedStatementCreatorFactory帮我吗?
那么换句话说,我如何正确地创建一个选择查询,该查询使用带有Spring JDBCTemplate的预准备语句来真正获得性能优势而不会失去RowMapper的优势?
| 归档时间: |
|
| 查看次数: |
16324 次 |
| 最近记录: |