我在 Spring Dao 中有以下代码,它工作得很好 -
Object args[] = { userId, restaurantId };
int userOrderCount = getJdbcTemplate()
.queryForInt(
"SELECT COUNT(orderid) FROM orders WHERE useridfk_order = ? AND restaurantidfk_order = ?",
args
);
但是,如果我决定按如下方式使用 NamedParameters 进行查询 -
int userOrderCount = getNamedParameterJdbcTemplate()
.queryForInt(
"SELECT COUNT(orderid) FROM orders WHERE useridfk_order = :userId AND restaurantidfk_order = :restaurantId",
new MapSqlParameterSource(":restaurantId", restaurantId)
.addValue(":userId", userId)
);
我收到这个异常 -
org.springframework.dao.InvalidDataAccessApiUsageException: No value supplied for the SQL parameter 'userId': No value registered for key 'userId'.
我知道金句“如果它没有坏就不要修理它”。
但是,我还是忍不住想知道为什么会这样?
最佳答案
使用这个。
new MapSqlParameterSource("restaurantId", restaurantId)
.addValue("userId", userId);
而不是这个。
new MapSqlParameterSource(":restaurantId", restaurantId)
.addValue(":userId", userId);
关于java - 如何将多个命名参数传递给 Spring JdbcTemplate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10135636/