@Repository public class UserDao { @Autowired JdbcTemplate jdbcTemplate;
public int addUser(User user){ return jdbcTemplate.update("INSERT INTO t_user(username,jobs,phone) VALUE (?,?,?)", user.getName(),user.getJobs(),user.getPhone()); } public int updateUser(User user){ return jdbcTemplate.update("UPDATE t_user SET username=?,jobs=?,phone=? WHERE id=?", user.getName(),user.getJobs(),user.getPhone(),user.getId()); } public int deleteUser(Integer id){ return jdbcTemplate.update("DELETE FROM t_user WHERE id=?",id); } public User getUserById(Integer id){ return jdbcTemplate.queryForObject("SELECT * FROM t_user WHERE id =?",new BeanPropertyRowMapper<>(User.class),id); } public List<User> getAllUser(){ return jdbcTemplate.query("SELECT * FROM t_user",new BeanPropertyRowMapper<>(User.class)); } }
@Autowired UserDao userDao; public int addUser(User user){ return userDao.addUser(user); } public int updateUser(User user){ return userDao.updateUser(user); } public int deleteUser(Integer id){ return userDao.deleteUser(id); } public User getUserById(Integer id){ return userDao.getUserById(id); } public List<User> getAllUser(){ return userDao.getAllUser(); } }
最后是UserController类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
@RestController public class UserController { @Autowired UserService userService; @GetMapping("/user") public void UserOps(){ User u1 = new User(); u1.setName("章三三"); u1.setJobs("程序员"); u1.setPhone("131444224433"); int i = userService.addUser(u1); System.out.println(i); List<User> allUser = userService.getAllUser(); System.out.println(allUser); } }
@Mapper public interface UserMapper { int addUser(User user); int deleteUser(int id); int updateUser(User user); User getUserById(Integer id); List<User> getAllUsers();
@Autowired UserMapper userMapper; public int addUser(User user){ return userMapper.addUser(user); } public int updateUser(User user){ return userMapper.updateUser(user); } public int deleteUser(Integer id){ return userMapper.deleteUser(id); } public User getUserById(Integer id){ return userMapper.getUserById(id); } public List<User> getAllUser(){ return userMapper.getAllUsers(); } }
@RestController public class UserController { @Autowired UserService userService; @GetMapping("/user") public void UserOps(){ User u1 = new User(); u1.setName("李思思"); u1.setJobs("美工"); u1.setPhone("131444224443"); int i = userService.addUser(u1); System.out.println(i); List<User> allUser = userService.getAllUser(); System.out.println(allUser); } }