1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| @Repository public class UsersDaoImpl implements UsersDao { @Autowired private JdbcTemplate jdbcTemplate; @Override public void insertUsers(Users users) { String sql = "insert into user(name,gender) values(?,?)"; this.jdbcTemplate.update(sql,users.getName(),users.getGender()); }
@Override public List<Users> selectUsersAll() { String sql = "select * from user"; return this.jdbcTemplate.query(sql, new RowMapper<Users>() { @Override public Users mapRow(ResultSet resultSet, int i) throws SQLException { Users users = new Users(); users.setId(resultSet.getInt("id")); users.setName(resultSet.getString("name")); users.setGender(resultSet.getString("gender")); return users; } }); }
@Override public Users selectUserById(Integer id) { Users user = new Users(); String sql = "select * from user where id = ?"; Object[] arry = new Object[]{id}; this.jdbcTemplate.query(sql, arry, new RowCallbackHandler() { @Override public void processRow(ResultSet resultSet) throws SQLException { user.setId(resultSet.getInt("id")); user.setName(resultSet.getString("name")); user.setGender(resultSet.getString("gender")); } }); return user; }
@Override public void updateUsers(Users users) { String sql = "update user set name = ?,gender = ? where id = ?"; this.jdbcTemplate.update(sql,users.getName(),users.getGender(),users.getId()); }
@Override public void deleteUserById(Integer id) { String sql = "delete from user where id = ?"; this.jdbcTemplate.update(sql,id); } }
|