当前位置:   article > 正文

从零开始完成一个SpringBoot+JPA的学生管理系统Web案例((二)后端实现)_助教管理系统的后端编辑

助教管理系统的后端编辑

这篇主要展示该案例的后端代码和功能实现,后端实现数据的操作功能后返回Json数组。

(一)目录结构:


(二)后台代码

Entity包

1. User.java

  1. package com.example.stu.kudestu.stu.entity;
  2. import javax.persistence.*;
  3. @Entity
  4. @Table(name = "user")
  5. public class User {
  6. @Id
  7. @GeneratedValue(strategy = GenerationType.IDENTITY) //设置主键自增
  8. private Integer id;
  9. private String username;
  10. private String password;
  11. public User() {
  12. }
  13. @Override
  14. public String toString() {
  15. return "User{" +
  16. "id=" + id +
  17. ", username='" + username + '\'' +
  18. ", password='" + password + '\'' +
  19. '}';
  20. }
  21. public Integer getId() {
  22. return id;
  23. }
  24. public void setId(Integer id) {
  25. this.id = id;
  26. }
  27. public String getUsername() {
  28. return username;
  29. }
  30. public void setUsername(String username) {
  31. this.username = username;
  32. }
  33. public String getPassword() {
  34. return password;
  35. }
  36. public void setPassword(String password) {
  37. this.password = password;
  38. }
  39. }

2. Student.java

  1. package com.example.stu.kudestu.stu.entity;
  2. import javax.persistence.*;
  3. @Entity
  4. @Table(name = "student")
  5. public class Student {
  6. @Id
  7. @GeneratedValue(strategy = GenerationType.IDENTITY)
  8. private Integer id;
  9. private String name;
  10. private Integer age;
  11. private String sex;
  12. public Student() {
  13. }
  14. @Override
  15. public String toString() {
  16. return "Student{" +
  17. "id=" + id +
  18. ", name='" + name + '\'' +
  19. ", age=" + age +
  20. ", sex='" + sex + '\'' +
  21. '}';
  22. }
  23. public Integer getId() {
  24. return id;
  25. }
  26. public void setId(Integer id) {
  27. this.id = id;
  28. }
  29. public String getName() {
  30. return name;
  31. }
  32. public void setName(String name) {
  33. this.name = name;
  34. }
  35. public Integer getAge() {
  36. return age;
  37. }
  38. public void setAge(Integer age) {
  39. this.age = age;
  40. }
  41. public String getSex() {
  42. return sex;
  43. }
  44. public void setSex(String sex) {
  45. this.sex = sex;
  46. }
  47. }

Dao包

1. UserDao

  1. package com.example.stu.kudestu.stu.dao;
  2. import com.example.stu.kudestu.stu.entity.User;
  3. import org.springframework.data.jpa.repository.JpaRepository;
  4. import org.springframework.data.jpa.repository.Query;
  5. import org.springframework.data.repository.query.Param;
  6. public interface UserDao extends JpaRepository<User,Integer> {
  7. User findUserById(Integer id);
  8. @Query(name="login",nativeQuery = true,value = "select * from user where username=:username and password=:password")
  9. User login(@Param("username") String username, @Param("password") String password);
  10. }

2. StudentDao

  1. package com.example.stu.kudestu.stu.dao;
  2. import com.example.stu.kudestu.stu.entity.Student;
  3. import com.example.stu.kudestu.stu.entity.User;
  4. import org.springframework.data.domain.Page;
  5. import org.springframework.data.jpa.repository.JpaRepository;
  6. import org.springframework.data.jpa.repository.Query;
  7. import org.springframework.data.repository.query.Param;
  8. import java.util.List;
  9. public interface StudentDao extends JpaRepository<Student,Integer> {
  10. Student findStuById(Integer id);
  11. @Query(name = "findStuByName",nativeQuery = true,value =
  12. "select * from student where name=:name")
  13. List<Student> findStuByName(@Param("name") String name);
  14. }

Service包

1. UserService 

  1. package com.example.stu.kudestu.stu.service;
  2. import com.example.stu.kudestu.stu.entity.User;
  3. import org.springframework.data.domain.Page;
  4. import java.util.List;
  5. public interface UserService {
  6. List<User> findAll();
  7. User findUserById(int id);
  8. User save(User user);
  9. User update(User user);
  10. void delete(int id );
  11. User login(String username,String password);
  12. Page<User> findByPage(int page, int pagesize);
  13. }

2. UserServiceImpl

  1. package com.example.stu.kudestu.stu.service;
  2. import com.example.stu.kudestu.stu.dao.UserDao;
  3. import com.example.stu.kudestu.stu.entity.User;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.domain.Page;
  6. import org.springframework.data.domain.PageRequest;
  7. import org.springframework.data.domain.Pageable;
  8. import org.springframework.stereotype.Service;
  9. import java.util.List;
  10. @Service
  11. public class UserServiceImpl implements UserService{
  12. @Autowired
  13. UserDao userDao;
  14. @Override
  15. public List<User> findAll() {
  16. return userDao.findAll();
  17. }
  18. @Override
  19. public User findUserById(int id) {
  20. return userDao.findUserById(id);
  21. }
  22. @Override
  23. public User save(User user) {
  24. return userDao.save(user);
  25. }
  26. @Override
  27. public User update(User user) {
  28. return userDao.save(user);
  29. }
  30. @Override
  31. public void delete(int id) {
  32. userDao.deleteById(id);
  33. }
  34. @Override
  35. public User login(String username, String password) {
  36. return userDao.login(username,password);
  37. }
  38. @Override
  39. public Page<User> findByPage(int page, int pagesize) {
  40. Pageable pageable = PageRequest.of(page,pagesize);
  41. return userDao.findAll(pageable);
  42. }
  43. }

3. StudentService

  1. package com.example.stu.kudestu.stu.service;
  2. import com.example.stu.kudestu.stu.entity.Student;
  3. import org.springframework.data.domain.Page;
  4. import java.util.List;
  5. public interface StudentService {
  6. //保存
  7. Student save(Student student);
  8. //修改
  9. Student update(Student student);
  10. //删除
  11. void delete(Integer id);
  12. //根据id查询
  13. Student findStuById(Integer id);
  14. //根据名字查询
  15. List<Student> fingStuByName(String name);
  16. /**
  17. * 分页查询所有
  18. * @param page
  19. * @param pagesize
  20. * @return
  21. */
  22. Page<Student> findAll(int page,int pagesize);
  23. }

4. StudentServiceImol

  1. package com.example.stu.kudestu.stu.service;
  2. import com.example.stu.kudestu.stu.dao.StudentDao;
  3. import com.example.stu.kudestu.stu.entity.Student;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.domain.Page;
  6. import org.springframework.data.domain.PageRequest;
  7. import org.springframework.data.domain.Pageable;
  8. import org.springframework.stereotype.Service;
  9. import java.util.List;
  10. @Service
  11. public class StudentServiceImpl implements StudentService{
  12. @Autowired
  13. StudentDao studentDao;
  14. @Override
  15. public Student save(Student student) {
  16. return studentDao.save(student);
  17. }
  18. @Override
  19. public Student update(Student student) {
  20. return studentDao.save(student);
  21. }
  22. @Override
  23. public void delete(Integer id) {
  24. studentDao.deleteById(id);
  25. }
  26. @Override
  27. public Student findStuById(Integer id) {
  28. return studentDao.findStuById(id);
  29. }
  30. @Override
  31. public List<Student> fingStuByName(String name) {
  32. return studentDao.findStuByName(name);
  33. }
  34. @Override
  35. public Page<Student> findAll(int page, int pagesize) {
  36. Pageable pageable = PageRequest.of(page,pagesize);
  37. return studentDao.findAll(pageable);
  38. }
  39. }

Controller包

1. UserController

  1. package com.example.stu.kudestu.stu.controller;
  2. import com.example.stu.kudestu.stu.entity.User;
  3. import com.example.stu.kudestu.stu.service.UserService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.domain.Page;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.util.List;
  12. @RestController
  13. @RequestMapping("/user")
  14. public class UserController {
  15. @Autowired
  16. private UserService userService;
  17. /**
  18. * 查询所有用户
  19. * @return
  20. */
  21. @RequestMapping("/findAll")
  22. public List<User> findAll(){
  23. return userService.findAll();
  24. }
  25. /**
  26. * 根据id查找用户
  27. * @param id
  28. * @return
  29. */
  30. @RequestMapping("/findById")
  31. public User findById(int id){
  32. return userService.findUserById(id);
  33. }
  34. /**
  35. * 用户注册
  36. * @param user
  37. * @return
  38. */
  39. @RequestMapping(value = "/reg",method = RequestMethod.POST)
  40. public User reg(User user){
  41. return userService.save(user);
  42. }
  43. /**
  44. * 用户登录
  45. * @param username
  46. * @param password
  47. * @return
  48. */
  49. @RequestMapping(value = "/login",method = RequestMethod.POST)
  50. public User login(String username,String password){
  51. return userService.login(username,password);
  52. }
  53. /**
  54. * 用户修改
  55. * @param user
  56. * @return
  57. */
  58. @RequestMapping(value = "/update",method = RequestMethod.POST)
  59. public User update(User user){
  60. return userService.save(user);
  61. }
  62. /**
  63. * 用户删除
  64. * @param id
  65. */
  66. @RequestMapping("/delete")
  67. public void delete(int id){
  68. userService.delete(id);
  69. }
  70. /**
  71. * 分页查询
  72. * @param page
  73. * @param response
  74. * @return
  75. */
  76. @RequestMapping("/page")
  77. public Page<User> findByPage(Integer page, HttpServletResponse response){
  78. //解决跨域请求
  79. response.setHeader("Access-Control-Allow-Origin","*");
  80. if(page==null || page<=0){
  81. page = 0;
  82. }else{
  83. page -= 1;
  84. }
  85. return userService.findByPage(page,5);
  86. }
  87. }

2. StudentController

  1. package com.example.stu.kudestu.stu.controller;
  2. import com.example.stu.kudestu.stu.entity.Student;
  3. import com.example.stu.kudestu.stu.service.StudentService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.domain.Page;
  6. import org.springframework.web.bind.annotation.*;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.util.List;
  9. @RestController
  10. @RequestMapping("/stu")
  11. public class StudentController {
  12. @Autowired
  13. StudentService studentService;
  14. @PostMapping("/add")
  15. public Student save(Student student){
  16. return studentService.save(student);
  17. }
  18. @PostMapping("/update")
  19. public Student update(Student student){
  20. return studentService.save(student);
  21. }
  22. @GetMapping("/delete/{id}")
  23. public String delete(@PathVariable int id){
  24. studentService.delete(id);
  25. return "delete success";
  26. }
  27. @GetMapping("/findById")
  28. public Student findById(int id){
  29. return studentService.findStuById(id);
  30. }
  31. @GetMapping("/findByName/{name}")
  32. public List<Student> findByName(@PathVariable String name){
  33. return studentService.fingStuByName(name);
  34. }
  35. @GetMapping("/query")
  36. public Page<Student> findByPage(Integer page, HttpServletResponse response){
  37. //解决跨域请求
  38. response.setHeader("Access-Control-Allow-Origin","*");
  39. if(page==null || page<=0){
  40. page = 0;
  41. }else{
  42. page -= 1;
  43. }
  44. return studentService.findAll(page,5);
  45. }
  46. }

(三)测试

这里使用Postman来测试(前后端分离为了测试Post请求和查看Json数据)下载Postman百度云盘链接  提取码:qe9r 

测试StudentControllar,功能已经全部实现和测试过了,下面选一些来展示。下面是一些请求Mapping:

  1. //学生注册
  2. @PostMapping("/add"
  3. //学生修改
  4. @PostMapping("/update")
  5. //学生删除
  6. @GetMapping("/delete/{id}")
  7. //根据Id查找学生
  8. @GetMapping("/findById")
  9. //根据姓名查找学生
  10. @GetMapping("/findByName/{name}")
  11. //分页查询所有
  12. @GetMapping("/query")

1. 测试注册

地址栏输入 http://localhost:8888/kude/stu/add 然后在下面添加参数后点击Send

添加后数据库信息:

2. 测试修改

成功后返回结果

3. 分页查询所有数据

此时表中共有14条数据,按照设定的一页五条数据的话,一共能分为三页。

输入请求地址

返回的所有Json数据如下:

  1. {
  2. "content": [
  3. {
  4. "id": 1,
  5. "name": "杜炮",
  6. "age": 20,
  7. "sex": "男"
  8. },
  9. {
  10. "id": 2,
  11. "name": "Lux",
  12. "age": 16,
  13. "sex": "女"
  14. },
  15. {
  16. "id": 3,
  17. "name": "Lux",
  18. "age": 17,
  19. "sex": "女"
  20. },
  21. {
  22. "id": 4,
  23. "name": "Lux",
  24. "age": 18,
  25. "sex": "女"
  26. },
  27. {
  28. "id": 5,
  29. "name": "Klua",
  30. "age": 20,
  31. "sex": "男"
  32. }
  33. ],
  34. "pageable": {
  35. "sort": {
  36. "sorted": false,
  37. "unsorted": true,
  38. "empty": true
  39. },
  40. "offset": 0,
  41. "pageNumber": 0,
  42. "pageSize": 5,
  43. "unpaged": false,
  44. "paged": true
  45. },
  46. "totalPages": 3, //分页数
  47. "totalElements": 14, //数据总条数
  48. "last": false,
  49. "number": 0, //当前页数(从零开始)0就是第一页
  50. "size": 5, //分页大小
  51. "sort": {
  52. "sorted": false,
  53. "unsorted": true,
  54. "empty": true
  55. },
  56. "numberOfElements": 5, //该页的数据条数
  57. "first": true,
  58. "empty": false
  59. }

测试完成,接下来编写前台代码,前后端链接再进行测试,请看下一篇:《从零开始完成一个SpringBoot+JPA的Web案例((三)前端实现)》 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/626996
推荐阅读
相关标签
  

闽ICP备14008679号