使用queryForObject时出现EmptyResultDataAccessException
EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
原代码
@Override
public User findByUsernameAndPassword(String username, String password) {
String sql = "select * from tab_user where username=? and password=?";
User user = template.queryForObject(sql,new BeanPropertyRowMapper<>(User.class),username,password);
return user;
}
or
@Override
public User findByUsernameAndPassword(String username, String password) {
User user = null;
try {
String sql = "select * from tab_user where username=? and password=?";
user = template.queryForObject(sql,new BeanPropertyRowMapper<>(User.class),username,password);
} catch (DataAccessException e) {
e.printStackTrace();
}
return user;
解决方法
@Override
public User findByUsernameAndPassword(String username, String password) {
User user = null;
try {
String sql = "select * from tab_user where username=? and password=?";
user = template.queryForObject(sql,new BeanPropertyRowMapper<>(User.class),username,password);
} catch (DataAccessException e) {
}
return user;
}