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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| package com.course.dao.impl;
import java.sql.ResultSet; import java.sql.SQLException; import java.util.Date; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository;
import com.course.dao.UserDao; import com.course.vo.User;
@Repository public class UserDaoImpl implements UserDao {
@Autowired private JdbcTemplate jdbcTemplate; @Override public int addUser(User user) { String sqlInsert = "insert into User(name, age, address, hire_date) values(?, ?, ?, ?)"; Object[] paramList = {user.getName(), user.getAge(), user.getAddress(), user.getHireDate()}; int rowCount = jdbcTemplate.update(sqlInsert, paramList); return rowCount; }
@Override public int updateUser(User user) { String sqlUpdate = "update User set age = ?, address = ?, hire_date = ? where name = ?"; Object[] paramList = {user.getAge(), user.getAddress(), user.getHireDate(), user.getName()}; int rowCount = jdbcTemplate.update(sqlUpdate, paramList); return rowCount; }
@Override public int deleteUser(User user) { String sqlDelete = "delete from User where name = ? and address = ?"; Object[] paramList = {user.getName(), user.getAddress()}; int rowCount = jdbcTemplate.update(sqlDelete, paramList); return rowCount; }
@Override public int queryCount() { String sql = "select count(*) from User"; int rowCount = jdbcTemplate.queryForObject(sql, Integer.class); return rowCount; }
@Override public User queryUserById(Integer id) { String sql = "select * from User where id = ?"; Object[] paramList = {id}; User user = jdbcTemplate.queryForObject(sql, paramList, new RowMapper<User>() {
@Override public User mapRow(ResultSet resultSet, int rowNum) throws SQLException { Integer UserId = resultSet.getInt("id"); String name = resultSet.getString("name"); int age = resultSet.getInt("age"); String address = resultSet.getString("address"); Date hireDate = resultSet.getDate("hire_date"); User userObj = new User(UserId, name, age, address, hireDate); return userObj; } }); return user; }
@Override public List<User> queryAllUser() { String sql = "select * from User"; List<User> userList = jdbcTemplate.query(sql, new RowMapper<User>() {
@Override public User mapRow(ResultSet resultSet, int rowNum) throws SQLException { Integer UserId = resultSet.getInt("id"); String name = resultSet.getString("name"); int age = resultSet.getInt("age"); String address = resultSet.getString("address"); Date hireDate = resultSet.getDate("hire_date"); User user = new User(UserId, name, age, address, hireDate); return user; } }); return userList; }
@Override public List<User> queryUserByPage(int pageIndex, int pageSize) { String sql = "select * from User limit ?, ?"; Object[] paramList = {(pageIndex-1)*pageSize, pageSize}; List<User> userList = jdbcTemplate.query(sql, paramList, new RowMapper<User>() {
@Override public User mapRow(ResultSet resultSet, int rowNum) throws SQLException { Integer UserId = resultSet.getInt("id"); String name = resultSet.getString("name"); int age = resultSet.getInt("age"); String address = resultSet.getString("address"); Date hireDate = resultSet.getDate("hire_date"); User user = new User(UserId, name, age, address, hireDate); return user; } }); return userList; } }
|