我在开发过程中感到疑惑
哪个更有效PreparedStatementSetter(Object)和PreparedStatementCreator(Object)并在spring框架中安全?
答案 0 :(得分:1)
您可以同时使用两者。 PreparedStatementCreator用于创建PreparedStatement对象。然后PreparedStatementSetter设置参数值。
String machaceksName = (String) jdbcTemplate.query(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
return connection
.prepareStatement("select first_name from customer where last_name like ?");
}
}, new PreparedStatementSetter() {
public void setValues(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setString(1, "Mach%");
}
}, new ResultSetExtractor() {
public Object extractData(ResultSet resultSet) throws SQLException, DataAccessException {
if (resultSet.next()) {
return resultSet.getLong(1);
}
return null;
}
});
我更倾向于传递Prepared Statement SQL查询并使用PreparedStatementSetter来设置参数值,因为它将最小化创建PreparedStatementCreator的时间/内存花费。
int r = jdbcTemplate.update(SQL_INSERT, new PreparedStatementSetter() {};