Spring jdbc 返回对象的写法

public int getMatchCount(String userName, String password) {
String sqlStr = " SELECT count(*) FROM t_user "
+ " WHERE user_name =? and password=? ";
return jdbcTemplate.queryForInt(sqlStr, new Object[] { userName, password });
}


public User findUserByUserName(final String userName) {
String sqlStr = " SELECT user_id,user_name,credits "
+ " FROM t_user WHERE user_name =? ";
final User user = new User();
jdbcTemplate.query(sqlStr, new Object[] { userName },
new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
user.setUserId(rs.getInt("user_id"));
user.setUserName(userName);
user.setCredits(rs.getInt("credits"));
}
});
return user;
}


public void updateLoginInfo(User user) {
String sqlStr = " UPDATE t_user SET last_visit=?,last_ip=?,credits=? "
+ " WHERE user_id =?";
jdbcTemplate.update(sqlStr, new Object[] { user.getLastVisit(),
user.getLastIp(),user.getCredits(),user.getUserId()});
}

你可能感兴趣的:(SpringJDBC)