• Java项目:springboot在线选课系统


    作者主页:夜未央5788

     简介:Java领域优质创作者、Java项目、学习资料、技术互助

    文末获取源码

    项目介绍

    后端技术包含springboot+mybatis+spring security+mysql+redis

    前端技术包含 semanticUI + thymeleaf模板引擎

    使用教程

    1.  下载项目之后 等待maven安装对应jar包
    2.  自行下载redis 并按照资源包下的application.yml要求进行配置

    3.  自行安装MySQL数据库 执行资源包下的sql文件

    使用说明

    1.  运行redis服务器
    2.  启动项目
    3.  访问localhost:8080

    4.  用户名:admin  密码:admin

    注意事项

    若导出信息时报错,则需要设置mysql,设置方式如下:

    SELECT @@sql_mode; 查看是否包含ONLY_FULL_GROUP_BY;若包含,则执行以下命令:

    SET GLOBAL sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

    SET SESSION sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

    执行完成后,再通过SELECT @@sql_mode; 来查看;
     

    注意:该方法仅用于临时修改,重启mysql后,以上设置失效。

    运行截图

     

     

     

     

     

     

    代码相关

    学生选课接口

    1. @RestController
    2. @RequestMapping("usercourse")
    3. @Api
    4. public class UserCourseController{
    5. @Autowired
    6. UserCourseService userCourseService;
    7. @Autowired
    8. SelectableCourseDAO selectableCourseDAO;
    9. /**
    10. * 选课
    11. * @param courseId
    12. * @param username
    13. * @return
    14. */
    15. @PostMapping("choose")
    16. @PreAuthorize("hasAuthority('student')")
    17. public Object chooseCourse(@RequestParam("courseId") Integer courseId ,
    18. @RequestParam("username") String username){
    19. Map map = new HashMap<>();
    20. try{
    21. return userCourseService.chooseCourse(courseId , username);
    22. }catch(Exception e){
    23. if(e instanceof DataIntegrityViolationException){
    24. map.put("msg","该课程已经被抢完啦。");
    25. }else{
    26. map.put("msg","出现其他异常,选课失败!");
    27. }
    28. map.put("flag",false);
    29. return JSON.toJSON(map);
    30. }
    31. }
    32. /**
    33. * 退课
    34. * @param courseId
    35. * @param username
    36. * @return
    37. */
    38. @PostMapping("cancel")
    39. @PreAuthorize("hasAuthority('student')")
    40. public Object cancelCourse(@RequestParam("courseId") Integer courseId ,
    41. @RequestParam("username") String username){
    42. return userCourseService.cancelCourse(courseId,username);
    43. }
    44. /**
    45. * 获取学生所选全部课程
    46. * @param page
    47. * @param limit
    48. * @param username
    49. * @return
    50. */
    51. @PostMapping("studentInfo")
    52. @PreAuthorize("hasAuthority('admin') or hasAuthority('student')")
    53. public Object studentInfo(@RequestParam(value = "page", defaultValue = "1") int page ,
    54. @RequestParam(value = "limit", defaultValue = "10") int limit ,
    55. @RequestParam("username")String username){
    56. try{
    57. Map map = new HashMap<>();
    58. PageHelper.startPage(page , limit);
    59. List list = selectableCourseDAO.selectByUser(username);
    60. if(list == null){
    61. return Msg.fail();
    62. }
    63. //System.out.println("=="+username+"==");
    64. PageInfo pageInfo = new PageInfo<>(list);
    65. map.put("totalPage" , pageInfo.getPages()); //总页数
    66. map.put("totalCount" , pageInfo.getTotal()); //总条数
    67. map.put("currentPage" , page); //当前页数。
    68. map.put("data" , pageInfo.getList()); //获得的数据量
    69. map.put("tCase",username);
    70. return JSON.toJSON(map);
    71. }catch(Exception e){
    72. e.printStackTrace();
    73. return Msg.fail();
    74. }
    75. }
    76. //测试。
    77. @PostMapping("cc")
    78. public Object cc(){
    79. Map map = new HashMap<>();
    80. try{
    81. selectableCourseDAO.updateMinCourseStock(1);
    82. return true;
    83. }catch(Exception e){
    84. if(e instanceof DataIntegrityViolationException){
    85. map.put("msg","该课程已经被抢完啦。");
    86. }else{
    87. map.put("msg","出现其他异常,选课失败!");
    88. }
    89. map.put("flag",false);
    90. return JSON.toJSON(map);
    91. }
    92. }
    93. }

    课程检索接口

    1. @RestController
    2. @RequestMapping("/course")
    3. @Api
    4. public class SelectableCourseController{
    5. @Autowired
    6. SelectableCourseService selectableCourseService;
    7. /**
    8. * 获得全部课程
    9. * @param page
    10. * @param limit
    11. * @param username
    12. * @return
    13. */
    14. @PostMapping("/getAll")
    15. public Object getAll(@RequestParam(value = "page", defaultValue = "1") int page ,
    16. @RequestParam(value = "limit", defaultValue = "10") int limit ,
    17. @RequestParam(value = "username", required = false) String username){
    18. return selectableCourseService.selectAll(page,limit,username);
    19. }
    20. /**
    21. * 根据课程类别搜索课程
    22. * @param page
    23. * @param limit
    24. * @param username
    25. * @param type
    26. * @return
    27. */
    28. @PostMapping("/getCourseByType")
    29. public Object getCourseByType(@RequestParam(value = "page", defaultValue = "1") int page ,
    30. @RequestParam(value = "limit", defaultValue = "10") int limit ,
    31. @RequestParam(value = "username", required = false) String username ,
    32. @RequestParam("courseType") String type){
    33. return selectableCourseService.selectCoursesByType(page,limit,type , username);
    34. }
    35. /**
    36. * 根据课程所属学院名称搜索课程
    37. * @param page
    38. * @param limit
    39. * @param username
    40. * @param name
    41. * @return
    42. */
    43. @PostMapping("/getCourseByCollege")
    44. public Object getCourseByCollege(@RequestParam(value = "page", defaultValue = "1") int page ,
    45. @RequestParam(value = "limit", defaultValue = "10") int limit ,
    46. @RequestParam(value = "username", required = false) String username ,
    47. @RequestParam("college") String name){
    48. return selectableCourseService.selectCoursesByCollege(page,limit,name , username);
    49. }
    50. /**
    51. * 根据课程名称模糊搜索课程
    52. * @param page
    53. * @param limit
    54. * @param username
    55. * @param courseName
    56. * @return
    57. */
    58. @PostMapping("selectByCourseName")
    59. public Object selectByCourseName(@RequestParam(value = "page", defaultValue = "1") int page ,
    60. @RequestParam(value = "limit", defaultValue = "10") int limit ,
    61. @RequestParam(value = "username", required = false) String username ,
    62. @RequestParam("courseName") String courseName){
    63. return selectableCourseService.selectByCourseName(page,limit,courseName , username);
    64. }
    65. /**
    66. * 根据课程剩余人数查找课程
    67. * @param page
    68. * @param limit
    69. * @param username
    70. * @param count
    71. * @return
    72. */
    73. @PostMapping("selectCourseByMemberCount")
    74. public Object selectByMemberCount(@RequestParam(value = "page", defaultValue = "1") int page ,
    75. @RequestParam(value = "limit", defaultValue = "10") int limit ,
    76. @RequestParam(value = "username", required = false) String username ,
    77. @RequestParam("count") Integer count){
    78. return selectableCourseService.selectCoursesByMemberCount(page,limit,count,username);
    79. }
    80. /**
    81. * 隐藏课程
    82. * @param courseId
    83. * @return
    84. */
    85. @PostMapping("hideBatch")
    86. @PreAuthorize("hasAuthority('admin')")
    87. public Object hideBatch(Integer courseId){
    88. try{
    89. return selectableCourseService.hideBatch(courseId);
    90. }catch(Exception e){
    91. return Msg.msg("操作异常!");
    92. }
    93. }
    94. @PostMapping("/addCourseByAdmin")
    95. @PreAuthorize("hasAuthority('admin')")
    96. public Object addCourse(@RequestParam("courseName")String courseName,
    97. @RequestParam("courseType")String courseType,
    98. @RequestParam("collegeId")Integer collegeId,
    99. @RequestParam("teacher")String teacher,
    100. @RequestParam("score")Integer score,
    101. @RequestParam("stock")Integer stock,
    102. @RequestParam("address")String address,
    103. @RequestParam(value = "description",defaultValue = "")String description){
    104. try{
    105. return selectableCourseService.addCourse(courseName,collegeId,courseType,teacher,score,stock,address,description);
    106. }catch(Exception e){
    107. e.printStackTrace();
    108. return Msg.msg("出现异常,添加课程失败!");
    109. }
    110. }
    111. }

    管理员接口

    1. @Controller
    2. @Api
    3. public class AdminController{
    4. @Autowired
    5. AdminService adminService;
    6. /**
    7. * Excel表格导出接口
    8. * http://localhost:8080/ExcelDownload
    9. * @param response response对象
    10. */
    11. @GetMapping("/ExcelDownload")
    12. @PreAuthorize("hasAuthority('admin')")
    13. public void excelDownload(HttpServletResponse response) throws IOException{
    14. adminService.excelOut(response);
    15. }
    16. /**
    17. * 课程管理
    18. * @return
    19. */
    20. @GetMapping("/courseManage")
    21. @PreAuthorize("hasAnyAuthority('admin')")
    22. public String courseManage(){
    23. return "courseManage";
    24. }
    25. /**
    26. * 添加课程
    27. * @return
    28. */
    29. @GetMapping("/addCourse")
    30. @PreAuthorize("hasAuthority('admin')")
    31. public String addCourse(){
    32. return "addCourse";
    33. }
    34. }

    登录接口

    1. @Controller
    2. @Api
    3. public class LoginController{
    4. @Autowired
    5. AdminService adminService;
    6. @Autowired
    7. UserService userService;
    8. @RequestMapping("/login")
    9. public String login(){
    10. return "login";
    11. }
    12. @GetMapping("/")
    13. public String index() {
    14. return "success";
    15. }
    16. @GetMapping("/manager")
    17. @PreAuthorize("hasAuthority('admin')")
    18. public String manager(){
    19. return "manager";
    20. }
    21. //@RequestMapping("/error")
    22. //public String error(){
    23. // return "error";
    24. //}
    25. @GetMapping("/info")
    26. @PreAuthorize("hasAuthority('student')")
    27. public String info(){
    28. return "studentInfo";
    29. }
    30. @GetMapping("/getCode")
    31. @ResponseBody
    32. public Object getCode(HttpServletRequest request) {
    33. /* 生成验证码字符串 */
    34. String verifyCode = VerifyCodeUtil.generateVerifyCode(4);
    35. String uuid = UUIDUtil.GeneratorUUIDOfSimple();
    36. HttpSession session = request.getSession();
    37. session.setAttribute(uuid,verifyCode); //将验证码与生成的uuid绑定在一起
    38. System.out.println("生成的验证码为:" + verifyCode);
    39. int width = 111,height = 36;
    40. try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
    41. VerifyCodeUtil.outputImage(width, height, stream, verifyCode);
    42. return Msg.msg("data",new ImgVO("data:image/gif;base64,"+ Base64Utils.encodeToString(stream.toByteArray()),uuid));
    43. } catch (IOException e) {
    44. e.printStackTrace();
    45. }
    46. return null;
    47. }
    48. public User getUser() { //为了session从获取用户信息,可以配置如下
    49. User user = new User();
    50. SecurityContext ctx = SecurityContextHolder.getContext();
    51. Authentication auth = ctx.getAuthentication();
    52. if (auth.getPrincipal() instanceof UserDetails) user = (User) auth.getPrincipal();
    53. return user;
    54. }
    55. public HttpServletRequest getRequest() {
    56. return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    57. }
    58. }

    如果也想学习本系统,下面领取。回复:017springboot

  • 相关阅读:
    NAT地址转换
    一个免杀项目分享
    RecAgent:A Novel Simulation Paradigm for Recommender Systems学习笔记
    吴恩达《机器学习》8-5->8-6:特征与直观理解I、样本与值观理解II
    OpenShift 4 - 部署 RHODS 环境,运行 AI/ML 应用(视频)
    数据结构——哈希
    基于ESP32的蓝牙鼠标键盘(一)BleKeyboard.h函数解析
    Linux上防火墙操作
    文件IO-缓冲区
    金融计量学实验报告一
  • 原文地址:https://blog.csdn.net/hanyunlong1989/article/details/125940845