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后,以上设置失效。

运行截图

 

 

 

 

 

 

代码相关

学生选课接口

@RestController
@RequestMapping("usercourse")
@Api
public class UserCourseController{

    @Autowired
    UserCourseService userCourseService;
    @Autowired
    SelectableCourseDAO selectableCourseDAO;

    /**
     * 选课
     * @param courseId
     * @param username
     * @return
     */
    @PostMapping("choose")
    @PreAuthorize("hasAuthority('student')")
    public Object chooseCourse(@RequestParam("courseId") Integer courseId ,
                               @RequestParam("username") String username){
        Map<String,Object> map = new HashMap<>();
        try{
            return userCourseService.chooseCourse(courseId , username);
        }catch(Exception e){
            if(e instanceof DataIntegrityViolationException){
                map.put("msg","该课程已经被抢完啦。");
            }else{
                map.put("msg","出现其他异常,选课失败!");
            }
            map.put("flag",false);
            return JSON.toJSON(map);
        }
    }

    /**
     * 退课
     * @param courseId
     * @param username
     * @return
     */
    @PostMapping("cancel")
    @PreAuthorize("hasAuthority('student')")
    public Object cancelCourse(@RequestParam("courseId") Integer courseId ,
                               @RequestParam("username") String username){
        return userCourseService.cancelCourse(courseId,username);
    }

    /**
     * 获取学生所选全部课程
     * @param page
     * @param limit
     * @param username
     * @return
     */
    @PostMapping("studentInfo")
    @PreAuthorize("hasAuthority('admin') or hasAuthority('student')")
    public Object studentInfo(@RequestParam(value = "page", defaultValue = "1") int page ,
                              @RequestParam(value = "limit", defaultValue = "10") int limit ,
                              @RequestParam("username")String username){
        try{
            Map<String,Object> map = new HashMap<>();
            PageHelper.startPage(page , limit);
            List<SelectableCourse> list = selectableCourseDAO.selectByUser(username);
            if(list == null){
                return Msg.fail();
            }
            //System.out.println("=="+username+"==");
            PageInfo<SelectableCourse> pageInfo = new PageInfo<>(list);
            map.put("totalPage" , pageInfo.getPages());  //总页数
            map.put("totalCount" , pageInfo.getTotal());  //总条数
            map.put("currentPage" , page);  //当前页数。
            map.put("data" , pageInfo.getList()); //获得的数据量
            map.put("tCase",username);
            return JSON.toJSON(map);
        }catch(Exception e){
            e.printStackTrace();
            return Msg.fail();
        }
    }
    
    //测试。
    @PostMapping("cc")
    public Object cc(){
        Map<String,Object> map = new HashMap<>();
        try{
            selectableCourseDAO.updateMinCourseStock(1);
            return true;
        }catch(Exception e){
            if(e instanceof DataIntegrityViolationException){
                map.put("msg","该课程已经被抢完啦。");
            }else{
                map.put("msg","出现其他异常,选课失败!");
            }
            map.put("flag",false);
            return JSON.toJSON(map);
        }
    }
}

课程检索接口

@RestController
@RequestMapping("/course")
@Api
public class SelectableCourseController{

    @Autowired
    SelectableCourseService selectableCourseService;

    /**
     * 获得全部课程
     * @param page
     * @param limit
     * @param username
     * @return
     */
    @PostMapping("/getAll")
    public Object getAll(@RequestParam(value = "page", defaultValue = "1") int page ,
                         @RequestParam(value = "limit", defaultValue = "10") int limit ,
                         @RequestParam(value = "username", required = false) String username){
        
        return selectableCourseService.selectAll(page,limit,username);
    }

    /**
     * 根据课程类别搜索课程
     * @param page
     * @param limit
     * @param username
     * @param type
     * @return
     */
    @PostMapping("/getCourseByType")
    public Object getCourseByType(@RequestParam(value = "page", defaultValue = "1") int page ,
                                  @RequestParam(value = "limit", defaultValue = "10") int limit ,
                                  @RequestParam(value = "username", required = false) String username ,
                                  @RequestParam("courseType") String type){
        return  selectableCourseService.selectCoursesByType(page,limit,type , username);
    }

    /**
     * 根据课程所属学院名称搜索课程
     * @param page
     * @param limit
     * @param username
     * @param name
     * @return
     */
    @PostMapping("/getCourseByCollege")
    public Object getCourseByCollege(@RequestParam(value = "page", defaultValue = "1") int page ,
                                     @RequestParam(value = "limit", defaultValue = "10") int limit ,
                                     @RequestParam(value = "username", required = false) String username ,
                                     @RequestParam("college") String name){
        return selectableCourseService.selectCoursesByCollege(page,limit,name , username);
    }

    /**
     * 根据课程名称模糊搜索课程
     * @param page
     * @param limit
     * @param username
     * @param courseName
     * @return
     */
    @PostMapping("selectByCourseName")
    public Object selectByCourseName(@RequestParam(value = "page", defaultValue = "1") int page ,
                                     @RequestParam(value = "limit", defaultValue = "10") int limit ,
                                     @RequestParam(value = "username", required = false) String username ,
                                     @RequestParam("courseName") String courseName){
        return selectableCourseService.selectByCourseName(page,limit,courseName , username);
    }

    /**
     * 根据课程剩余人数查找课程
     * @param page
     * @param limit
     * @param username
     * @param count
     * @return
     */
    @PostMapping("selectCourseByMemberCount")
    public Object selectByMemberCount(@RequestParam(value = "page", defaultValue = "1") int page ,
                                      @RequestParam(value = "limit", defaultValue = "10") int limit ,
                                      @RequestParam(value = "username", required = false) String username ,
                                      @RequestParam("count") Integer count){
        return selectableCourseService.selectCoursesByMemberCount(page,limit,count,username);
    }

    /**
     * 隐藏课程
     * @param courseId
     * @return
     */
    @PostMapping("hideBatch")
    @PreAuthorize("hasAuthority('admin')")
    public Object hideBatch(Integer courseId){
        try{
            return selectableCourseService.hideBatch(courseId);
        }catch(Exception e){
            return Msg.msg("操作异常!");
        }
    }

    @PostMapping("/addCourseByAdmin")
    @PreAuthorize("hasAuthority('admin')")
    public Object addCourse(@RequestParam("courseName")String courseName,
                            @RequestParam("courseType")String courseType,
                            @RequestParam("collegeId")Integer collegeId,
                            @RequestParam("teacher")String teacher,
                            @RequestParam("score")Integer score,
                            @RequestParam("stock")Integer stock,
                            @RequestParam("address")String address,
                            @RequestParam(value = "description",defaultValue = "")String description){
        try{
            return selectableCourseService.addCourse(courseName,collegeId,courseType,teacher,score,stock,address,description);
        }catch(Exception e){
            e.printStackTrace();
            return Msg.msg("出现异常,添加课程失败!");
        }
    }
}

管理员接口

@Controller
@Api
public class AdminController{

    @Autowired
    AdminService adminService;
    /**
     * Excel表格导出接口
     * http://localhost:8080/ExcelDownload
     * @param response response对象
     */
    @GetMapping("/ExcelDownload")
    @PreAuthorize("hasAuthority('admin')")
    public void excelDownload(HttpServletResponse response) throws IOException{
        adminService.excelOut(response);
    }

    /**
     * 课程管理
     * @return
     */
    @GetMapping("/courseManage")
    @PreAuthorize("hasAnyAuthority('admin')")
    public String courseManage(){
        return "courseManage";
    }

    /**
     * 添加课程
     * @return
     */
    @GetMapping("/addCourse")
    @PreAuthorize("hasAuthority('admin')")
    public String addCourse(){
        return "addCourse";
    }
}

登录接口

@Controller
@Api
public class LoginController{
    
    @Autowired
    AdminService adminService;
    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @GetMapping("/")
    public String index() {
        return "success";
    }
    
    
    @GetMapping("/manager")
    @PreAuthorize("hasAuthority('admin')")
    public String manager(){
        return "manager";
    }
    //@RequestMapping("/error")
    //public String error(){
    //    return "error";
    //}
    
    @GetMapping("/info")
    @PreAuthorize("hasAuthority('student')")
    public String info(){
        return "studentInfo";
    }

    @GetMapping("/getCode")
    @ResponseBody
    public Object getCode(HttpServletRequest request) {

        /* 生成验证码字符串 */
        String verifyCode = VerifyCodeUtil.generateVerifyCode(4);
        String uuid = UUIDUtil.GeneratorUUIDOfSimple();
        
        HttpSession session = request.getSession();
        session.setAttribute(uuid,verifyCode); //将验证码与生成的uuid绑定在一起
        System.out.println("生成的验证码为:" + verifyCode);

        int width = 111,height = 36;

        try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
            VerifyCodeUtil.outputImage(width, height, stream, verifyCode);
            return Msg.msg("data",new ImgVO("data:image/gif;base64,"+ Base64Utils.encodeToString(stream.toByteArray()),uuid));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public User getUser() { //为了session从获取用户信息,可以配置如下
        User user = new User();
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        if (auth.getPrincipal() instanceof UserDetails) user = (User) auth.getPrincipal();
        return user;
    }

    public HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜未央5788

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值