SpringMVC implements RESTful services
Only the code of the service and controller layers is mentioned here. The Mapper layer can directly inherit Mapper<T>, remember that mybatis-config.xml must configure the paging plugin and mapper class plugin.
1. Query all users
service:
1 @Autowired 2 private IUserMapper userMapper; 3 4 @Override 5 public EasyUIResult getAllUser(Integer page, Integer rows) { 6 // TODO Auto-generated method stub 7 8 // The background helps us encapsulate a page and store it directly in the local thread 9 PageHelper.startPage(page, rows); 10 Example example = new Example( User.class ); 11 example.setOrderByClause("updated desc" ); 12 List<User> list = userMapper.selectByExample(example); 13 PageInfo<User> pageInfo = new PageInfo<>(list); 14 return new EasyUIResult(pageInfo.getTotal(), pageInfo.getList()); 15 }
controller:
1 /* * 2 * Query all users The return data type is: json format 3 * 4 * @param page 5 * @param rows 6 * @return 7 */ 8 @RequestMapping(value = " list " , method = { RequestMethod. POST }) 9 public @ResponseBody EasyUIResult finAllUser(@RequestParam(value = " page " , defaultValue = " 1 " ) Integer page, 10 @RequestParam(value = " rows " , defaultValue ="5") Integer rows) { 11 return userService.getAllUser(page, rows); 12 }
2. Query user information based on user id
service:
1 @Override 2 public User queryUserById(Long id) { 3 // TODO Auto-generated method stub 4 return userMapper.selectByPrimaryKey(id); 5 }
cotroller:
1 @Controller 2 @RequestMapping("user/rest") 3 public class RestUserController { 4 5 @Autowired 6 private IUserService userService; 7 8 /** 9 * 根据id查询用户信息 10 * 11 * @param id 12 * @return 13 */ 14 @RequestMapping(value = "{userId}", method = RequestMethod.GET) 15 public ResponseEntity<User> queryUserById(@PathVariable(value = "userId") Long id) { 16 try { 17 User user = userService.queryUserById(id); 18 if (user == null) { 19 return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); 20 } 21 // return ResponseEntity.ok(user); 22 } catch (Exception e) { 23 // TODO: handle exception 24 e.printStackTrace(); 25 } 26 // return ResponseEntity.status(500).body(null); 27 // System internal exception 28 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body( null ); 29 }
3. Add a user
service:
1 @Override 2 public Boolean saveUser(User user) { 3 // TODO Auto-generated method stub 4 return userMapper.insertSelective(user) == 1; 5 }
controller:
1 /** 2 * 添加一个用户 3 * 4 * @param user 5 * @return 6 */ 7 @RequestMapping(method = RequestMethod.POST) 8 public ResponseEntity<Void> addUser(User user) { 9 try { 10 userService.saveUser(user); 11 return ResponseEntity.status(HttpStatus.CREATED).build(); 12 // return ResponseEntity.ok(user); 13 } catch (Exception e) { 14 // TODO: handle exception 15 e.printStackTrace(); 16 } 17 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); 18 }
4. Modify the user
service:
1 @Override 2 public boolean updateUsers(User user) { 3 // TODO Auto-generated method stub 4 // Modify all user information 5 // userMapper.updateByPrimaryKey(User); 6 // Modify non-empty fields 7 // userMapper.updateByPrimaryKeySelective(record); 8 return userMapper.updateByPrimaryKey(user) == 1 ; 9 }
controller:
/** * Follow up with new users * * @param user * @return */ @RequestMapping(method = RequestMethod.PUT) public ResponseEntity<Void> updateUser(User user) { try { boolean b = userService.updateUsers(user); if (b) { return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); } // return ResponseEntity.ok(user); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); }
5. Delete
service:
1 @Override 2 public boolean deleteUser(Long id) { 3 // TODO Auto-generated method stub 4 return userMapper.deleteByPrimaryKey(id) == 1; 5 }
controller:
1 /** 2 * 删除一个用户 3 * 4 * @param id 5 * @return 6 */ 7 @RequestMapping(method = RequestMethod.DELETE) 8 public ResponseEntity<Void> deleteUser(@RequestParam(value = "id", defaultValue = "0") Long id) { 9 try { 10 if (id == 0) { 11 return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); 12 } 13 boolean b = userService.deleteUser(id); 14 if (b) { 15 return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); 16 } 17 // return ResponseEntity.ok(user); 18 } catch (Exception e) { 19 // TODO: handle exception 20 e.printStackTrace(); 21 } 22 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); 23 }
You're done! !