spring-boot - PreparedStatement 未执行且 RowMapper 未调用
问题描述
对于简单的登录访问检查,我将 jdbcTemplate 与以下 DAO 方法一起使用:-
@Autowired
JdbcTemplate jdbcTemplate;
public List<User> hasAccess (String user, String pass) {
return jdbcTemplate.query(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement("SELECT * FROM SHB_USERS WHERE USER=? AND PASS=?");
ps.setString(1, user);
ps.setString(2, pass);
return ps;
}
}, new RowMapper<User>() {
@Override
public User mapRow(ResultSet resultSet, int rownum) throws SQLException {
String user = resultSet.getString("USER");
String pass = resultSet.getString("PASS");
User userObj = new User(user, pass);
System.out.println(userObj.toString());
return userObj;
}
});
}
但不知何故mapRow
不叫。PreparedStatement
例如,当我使用直接字符串而不是它时,select * from shb_users where rownum <2
它可以工作。我是 Spring Boot 新手,无法找出错误。
解决方案
尽管您已经在代码中发现了问题,但我仍会继续向最终访问此页面的其他用户提及它,同时找到来自mapRow(...)
.
@Nullable
T mapRow(ResultSet rs,
int rowNum)
throws SQLException
根据 Spring 的官方文档,
This method should not call next() on the ResultSet; it is only supposed to map values of the current row.
这解释了在此方法的任何给定执行中,cursor
参数内的参数ResultSet rs
位于由参数表示的单行rowNum
。如果在resultset
pass 处没有行rowNum
,则不会执行此方法。
推荐阅读
- electron - 在我的自定义传输中未调用日志函数
- android - 如何在android另一个活动的操作栏上显示后退按钮
- nginx - Nginx - 添加 server_name 后,所有 url 都显示 404
- reactjs - 使用 mqtt 的 HighCharts 实时数据
- html - html 中的常量,如 C/C++ 中的 #define
- ios - 当应用程序进入前台时显示视图控制器但不知道在 swift 4 中关闭此视图控制器
- javascript - 如何从两个库中导入两个同名的组件?
- asp.net-core - 如何从 .net 核心中的部分视图访问控制器?
- java - 反序列化为没有@type 字段的子类
- haskell - haskell ADT 选择正确的类型