We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
代码如下
public User getUserByName(String username) { DataSourceRouterManager.setCurrentDataSourceKey("primary"); try { return (User) jdbcTemplate.queryForObject( "select id,username,password,salt from user_table where username=? ", new Object[]{username}, new RowMapper() { @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { User user = new User(); user.setUsername((rs.getString("username"))); user.setPassword(rs.getString("password")); user.setSalt(rs.getString("salt")); user.setId(rs.getInt("id")); return user; } } ); } // 如果表中无对应记录,queryForObject会抛出异常,捕获后返回空对象 catch (EmptyResultDataAccessException e){ return null; } }
test方法如下
public String test() { String res = ""; String name = "viking"; User user= userDao.getUserByName(name); User user1 = userDao.getUserByName(name); res += user.toString()+user1.toString(); return res ; }
结果在user.setUsername((rs.getString("username")));行抛出空指针异常
user.setUsername((rs.getString("username")));
另外如果使用queryForMap则不会这样
public User getUserByName(String username) { DataSourceRouterManager.setCurrentDataSourceKey("primary"); try { Map<String, Object> map = jdbcTemplate.queryForMap( "select id,username,password,salt from user_table where username=? ", new Object[]{username}); User user = new User(); user.setUsername(map.get("username").toString()); user.setPassword(map.get("password").toString()); user.setSalt(map.get("salt").toString()); user.setId(Integer.parseInt(map.get("id").toString())); return user; } catch (EmptyResultDataAccessException e){ return null; } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
代码如下
test方法如下
结果在
user.setUsername((rs.getString("username")));
行抛出空指针异常另外如果使用queryForMap则不会这样
The text was updated successfully, but these errors were encountered: