在 Spring 3.2中不推荐使用 JdbcTemplate 中的 queryforInt/queryforLong 方法。我不能找出为什么或什么被认为是使用这些方法替换现有代码的最佳实践。
一个典型的方法:
int rowCount = jscoreJdbcTemplate.queryForInt(
"SELECT count(*) FROM _player WHERE nameKey = ? AND teamClub = ?",
playerNameKey.toUpperCase(),
teamNameKey.toUpperCase()
);
好的,上面的方法需要重写如下:
Object[] params = new Object[] {
playerNameKey.toUpperCase(),
teamNameKey.toUpperCase()
};
int rowCount = jscoreJdbcTemplate.queryForObject(
"SELECT count(*) FROM _player WHERE nameKey = ? AND teamClub = ?",
params, Integer.class);
Obviously this deprecation makes the JdbcTemplate class simpler (or does it?). QueryForInt was always a convenience method (I guess) and has been around a long time. Why has it been removed. The code becomes more complicated as a result.