SpringBoot + Servlet + Mybatis+ layui 学生选课管理系统
課程設(shè)計目的
本次課設(shè)做的系統(tǒng)為學(xué)生課程設(shè)計管理系統(tǒng),鑒于學(xué)校規(guī)模的不斷擴大,而且隨著每年的擴招,人數(shù)不斷的增加,每次課程設(shè)計都采用手工操作,費時費力。為了解決這個問題,決定做一個課程設(shè)計管理系統(tǒng),對每個學(xué)期選修課程設(shè)計的學(xué)生基本情況作個統(tǒng)計。學(xué)生可以在系統(tǒng)中注冊賬號,登錄后學(xué)生從該系統(tǒng)中選題,填寫同組學(xué)生的姓名,組長等基本情況;查詢自己的課程設(shè)計成績,課程設(shè)計的選擇情況,成績的合格情況,小組成員信息,上課程設(shè)計報告的上傳,下載,刪除,預(yù)覽等功能。老師通過該系統(tǒng)添加課設(shè)信息,查看學(xué)生的選題情況,給出學(xué)生的分?jǐn)?shù)。這有利于老師教學(xué),及時了解學(xué)生的情況,提高教學(xué)質(zhì)量,減輕老師的工作量,改善原有的比較繁鎖的工作。
題目基本情況
本系統(tǒng)涉及權(quán)限管理,以老師,學(xué)生分別登錄以進(jìn)行不同的操作,老師可以對查看學(xué)生信息,給出學(xué)生成績查看學(xué)生選題情況,添加課設(shè)信息。學(xué)生可以添加同組成員信息選擇課程設(shè)計,退掉課程設(shè)計,查看課程設(shè)計的成績,上傳實驗報告.為了提高信息的處理效率,設(shè)計一款小型的課程設(shè)計管理系統(tǒng)。將數(shù)據(jù)庫的內(nèi)容通過web服務(wù)器配合前端框架在瀏覽器展現(xiàn)出來,達(dá)到直觀,方便的管理目的。
數(shù)據(jù)庫表設(shè)計
教師屬性:教師號,姓名,職稱,密碼。
學(xué)生屬性:學(xué)號,姓名,班級,密碼。
題目屬性:課題號,課題目名稱,題目信息,教工號。
選題屬性:學(xué)號,題號,成績,組號。
小組屬性:組號,組員,組長。
文件屬性:文件號,文件名,后綴,路徑大小,類型,下載次數(shù),上傳時間,學(xué)號
技術(shù)棧
數(shù)據(jù)庫連接池技術(shù) ?maven工具????servlet技術(shù)
Ajax請求 ?Spring-Boot框架 ??Mybatis框架 ???JSON數(shù)據(jù)格式 ????????????????????????????????????????????????????????
?commons-fileupload組件 ?layui框架 ?
程序流程圖
?
?
5.3.1登錄操作
本程序設(shè)計的權(quán)限管理中,不同身份可以進(jìn)行的操作不一樣,因此不同的身份,決定執(zhí)行什么樣的操作,如圖6-1所示為教師登錄驗證界面,前端使用leyui框架布局,
驗證碼使用BufferedImage類繪制,用戶輸入信息后進(jìn)行后臺的邏輯判斷,成功則進(jìn)入到后臺管理界面。若用戶名和姓名和驗證碼輸入正確,則跳轉(zhuǎn)到對應(yīng)的請求映射,若勾選記住密碼,則會在信息全部正確匹配后將正確的信息存入cookie,若其中一項輸入錯誤,則會重新定向到登錄界面。
圖6-1教師登錄界面
圖6-2學(xué)生登錄界面
登錄部分代碼:
Dao層:@Select("SELECT * from student where stdname =#{stdname} and stdpassword =#{stdpassword}")public Student findstudentByNameAndPassword(Student student);//通過用戶的姓名和密碼查詢Controller層:@RequestMapping("/tologin")public String index() {return "login";}//登錄1@RequestMapping(value = "/login")public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response, Student student) throws Exception {String name = request.getParameter("stdname");String pwd = request.getParameter("stdpassword");String yzm = request.getParameter("captcha");String shenfen = request.getParameter("shenfen");HttpSession session = request.getSession();String code = (String) session.getAttribute("code");String rember = request.getParameter("rememberMe");//3.處理跳轉(zhuǎn)//創(chuàng)建ModelAndView對象ModelAndView mav = new ModelAndView();if (yzm.equals(code)) {Student student_find = studentService.login(student);System.out.println(student_find);if (student_find != null) {//1.辦卡String cusername = java.net.URLEncoder.encode(name, "utf-8");Cookie cookie1 = new Cookie("uname", cusername);Cookie cookie2 = new Cookie("passwd", pwd);//3.設(shè)置范圍/* cookie1.setPath("/mydemo3/");cookie2.setPath("/mydemo3/");*/if ("true".equals(rember)) {//2.設(shè)置時效(一次瀏覽器的關(guān)閉)cookie1.setMaxAge(60 * 60 * 24 * 10);cookie2.setMaxAge(60 * 60 * 24 * 10);} else {cookie1.setMaxAge(0);cookie2.setMaxAge(0);}//4.給客戶response.addCookie(cookie1);response.addCookie(cookie2);//向模型對象中添加數(shù)據(jù)mav.addObject("student", student_find);session.setAttribute("stulogin", student_find);session.setAttribute("studentname", student.getStdname());session.setAttribute("stdpwd", student.getStdpassword());//設(shè)置邏輯視圖名mav.setViewName("studentstage");//返回ModelAndView對象return mav;} else {mav.addObject("name", "請重新登錄!");//設(shè)置邏輯視圖名mav.setViewName("login");//返回ModelAndView對象return mav;}} else {mav.setViewName("login");}return mav;}驗證碼:public class CaptcahCode {public static String drawImageVerificate(HttpServletResponse response) {//定義驗證碼的寬度和高度int width = 120;int height = 25;//在內(nèi)存中創(chuàng)建圖片BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//創(chuàng)建圖片上下文Graphics2D g = image.createGraphics();//產(chǎn)生隨機對象,此隨機對象主要用于算術(shù)表達(dá)式的數(shù)字Random random = new Random();//設(shè)置背景g.setColor(getRandomColor(240, 250));//設(shè)置字體g.setFont(new Font("微軟雅黑", Font.PLAIN, 22));//開始繪制g.fillRect(0, 0, width, height);//干擾線的繪制,繪制干擾線到圖片中g(shù).setColor(getRandomColor(180, 230));for (int i = 0; i < 100; i++) {int x = random.nextInt(width);int y = random.nextInt(height);int x1 = random.nextInt(60);int y1 = random.nextInt(60);g.drawLine(x, y, x1, y1);}//開始進(jìn)行對算術(shù)驗證碼表達(dá)式的拼接int num1 = (int) (Math.random() * 10 + 1);int num2 = (int) (Math.random() * 10 + 1);int fuhao = random.nextInt(3);//產(chǎn)生一個0-2之間的隨機整數(shù)//記錄符號String fuhaostr = null;int result = 0;switch (fuhao) {case 0:fuhaostr = "+";result = num1 + num2;break;case 1:fuhaostr = "-";result = num1 - num2;break;case 2:fuhaostr = "*";result = num1 * num2;break;}//拼接算術(shù)表達(dá)式,用戶圖片顯示String calc = num1 + " " + fuhaostr + " " + num2 + " = ?";//設(shè)置隨機顏色g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));//繪制表達(dá)式g.drawString(calc, 5, 25);//結(jié)束繪制try {//輸出圖片到頁面ImageIO.write(image, "jpg", response.getOutputStream());return String.valueOf(result);} catch (Exception e) {e.printStackTrace();return null;}}public static Color getRandomColor(int fc, int bc) {//利用隨機數(shù)Random random = new Random();//隨機顏色,了解顏色-Color(red,green,blue).rgb三元色0-255if (fc > 255) fc = 255;if (bc > 255) bc = 255;int r = fc + random.nextInt(bc - fc);int g = fc + random.nextInt(bc - fc);int b = fc + random.nextInt(bc - fc);return new Color(r, g, b);}}5.3.2注冊操作
本程序設(shè)計的權(quán)限管理中,支持學(xué)生的注冊賬號功能。若點擊注冊,則進(jìn)行注冊的映射請求,輸入對應(yīng)信息,進(jìn)行注冊,所示選項全部為必填項,否則無法進(jìn)行注冊,兩次輸入密碼會進(jìn)行判斷,不同則注冊失敗。輸入的學(xué)號會事先判斷是否已經(jīng)存在,存在則返回已存在。
?
圖6-3注冊界面
注冊部分代碼
Dao層:@Insert("insert into student(stdid, stdname, stdpassword, stdclass) value(#{stdid}, #{stdname}, #{stdpassword},#{stdclass})")public int insertStudent(Student student);//注冊用戶@Select("SELECT COUNT(stdid) from student where stdid=#{stdid}")public int findStudentExist(String stdid);//用戶id查看是否已經(jīng)存在Controller層://注冊頁面顯示@RequestMapping("/register")public String registerUser() {return "register";}//注冊功能實現(xiàn)@RequestMapping(value = "/registerStudent", method = RequestMethod.POST)public String registerUser(Student student, HttpServletRequest request) throws Exception {if (!studentService.findStudentExist(student.getStdid())) {request.setAttribute("error", "用戶ID已經(jīng)存在!");request.setAttribute("stdid", student.getStdid());request.setAttribute("stdname", student.getStdname());request.setAttribute("stdpassword", student.getStdpassword());request.setAttribute("stdclass", student.getStdclass());return "register";} else {studentService.register(student);}return "tips";}5.3.3后臺選擇操作
學(xué)生登錄完畢后,將會進(jìn)入圖6-3-1所示的學(xué)生界面,學(xué)生可以查看個人信息,查看選課信息以及退課,修改密碼,查詢課設(shè)信息以及選課,成績查詢和報告的上傳,下載,預(yù)覽和刪除;教師登錄完畢后,將會進(jìn)入圖6-3-2所示的教師界面,教師可以查看個人信息,查看自己的開課信息以及加課,修改密碼,查詢學(xué)生選擇課程的信息以及給出學(xué)生成績。
?
圖6-3-1 學(xué)生后臺界面
?
圖6-3-2教師后臺界面
5.3.4個人中心界面
點擊個人中心的下拉框后,進(jìn)入如圖6-4-1的界面,通過session取出在登錄時存入的姓名密碼等信息,將查到的信息返回到前端界面進(jìn)行展示。用戶可以點擊小眼睛查看自己的密碼。
關(guān)鍵代碼:
Dao層:@Select("SELECT * FROM `student`where stdname =#{stdname}")public Student findStudentByName(String stdname) throws IOException;//根據(jù)用戶id查找用戶Controller層:@RequestMapping("udentcenter")public?String?center(HttpSession?session)?throws?IOException?{String?stdname?=?(String)?session.getAttribute("studentname");Student?student?=?studentService.findStudentByName(stdname);session.setAttribute("stu",?student);return?"studentcenter"; }圖6-4學(xué)生個人中心界面
5.3.5學(xué)生課設(shè)中心界面
點擊課設(shè)中心下拉框,請求映射到對應(yīng)的地址,通過登錄時存放的session,取出登錄用戶的id,調(diào)用service層函數(shù),service層調(diào)用dao層的函數(shù),操作數(shù)據(jù)庫取出所需的值,并存放在list集合中,返回該頁面。對于還沒出成績的課程,學(xué)生有權(quán)選擇推掉這門課設(shè)。同時點擊課設(shè)名可以查看課設(shè)的詳細(xì)信息,點擊組號可以查看小組的詳細(xì)信息。
主要代碼:
Dao層:@Select("SELECTquesion.queid,quesion.quename,teacher.teaname,choque.gruid,choque.score from quesion,teacher,choque\n" +"where quesion.queid = choque.queid ?and teacher.teaid = quesion.teaid and choque.stdid = #{stdid}")public List<ChooseCourseInfo> findChooseCourseInfo(String stdid);@Select("SELECT queid,quename,quemess FROM quesion where queid = #{queid}")public Quesion showcourse(String queid);@Select("SELECT `group`.grubody ,`group`.gruhead from `group` where gruid =#{gruid}")public Group showgroup(String gruid);Controller層:@RequestMapping("dcourse")public?String?stdcourse(HttpSession?session,?Model?model)?throws?IOException?{Student?stu?=?(Student)?session.getAttribute("stulogin");List<CourseInfo>?courseInfos?=?studentService.findCourseInfo(stu.getStdid());model.addAttribute("courseinfos",?courseInfos);return?"stdcourse";}@RequestMapping("owcourse")public?String?showcourse(String?queid,?Model?model)?throws?IOException?{Quesion?quesion?=?studentService.showcourse(queid);model.addAttribute("quesion",?quesion);return?"showcourse";}@RequestMapping("owgroup")public?String?showgroup(String?gruid,?Model?model)?throws?IOException?{Group?group?=?studentService.showgroup(gruid);model.addAttribute("group",?group);return?"showgroup";}?
圖6-5學(xué)生課設(shè)中心界面
5.3.6教師課設(shè)中心界面
點擊課設(shè)中心下拉框,請求映射到對應(yīng)的地址,通過登錄時存放的session,取出登錄用戶的id,調(diào)用service層函數(shù),service層調(diào)用dao層的函數(shù),操作數(shù)據(jù)庫取出所需的值,并存放在list集合中,返回該頁面,教師還可以對已經(jīng)添加的課設(shè)進(jìn)行刪除,也可以通過點擊添加課程設(shè)計按鈕,輸入信息后,添加新的課程設(shè)計。
主要代碼:
Dao層:@Select("select quesion.queid,quesion.quename,\n" +"(select COUNT(*) from choque where choque.queid = quesion.queid) as stucount, \n" +"(SELECT COUNT(*) FROM choque where choque.queid = quesion.queid and choque.score<60) as passfail\n" +"FROM quesion WHERE teaid = #{teaid}")public List<TeacherCourse> findteachercourse(String teaid);@Insert("insert into quesion (queid, quename, quemess, teaid) VALUES (#{queid},#{quename},#{quemess},#{teaid}) ")@Delete("delete from quesion where queid= #{queid}")public void dlelete(String queid);public void insertteacourse(Quesion quesion);Controller層:@GetMapping("/topasscourse")public?String?topasscourse()?{return?"teachercourse";}@GetMapping("owcourse")@ResponseBodypublic?Map<String,?Object>?coursecenter(HttpSession?session,HttpServletRequest?request)?throws?IOException?{ Teacher?teacher?=?(Teacher)?session.getAttribute("tealogin"); List<TeacherCourse>?teacherCourses?=?teacherService.FindTeacherCourse(teacher.getTeaid());Map<String,?Object>?res?=?new?HashMap<>();res.put("code",?0);res.put("data",?teacherCourses);return?res;}@RequestMapping("/toaddcourse")public String toaddcou() {return "addteacourse";}@RequestMapping("/addteachercourse")@ResponseBodypublic Map<String, String> addcourse(Model model, HttpServletRequest request, Quesion quesion, HttpSession session) throws IOException {Teacher teacher = (Teacher) session.getAttribute("tealogin");System.out.println(quesion);Map<String, String> res = new HashMap<>();if (!teacherService.findqueexist(quesion.getQueid())) {res.put("code","200");res.put("msg", "課號已存在,不能重復(fù)添加");}else{quesion.setTeaid(teacher.getTeaid());teacherService.insertteacourse(quesion);res.put("code", "0");res.put("msg", "添加成功");}return res;}@RequestMapping("/delcourse")public String delcourse(String gruid) throws IOException {studentService.delcourse(gruid);return "redirect:coursecenter"; }?
圖6-6學(xué)生課設(shè)中心界面
5.3.7修改密碼界面
點擊修改密碼下拉框,請求映射到對應(yīng)的地址,通過登錄時存放的session,取出登錄用戶的id,調(diào)用service層函數(shù),service層調(diào)用dao層的函數(shù),通過傳入的參數(shù)進(jìn)行對學(xué)生信息的更新,頁面部分,系統(tǒng)會對輸入的信息進(jìn)行判斷,包括原密碼和新密碼,判斷原密碼是否正確,判斷兩次輸入的密碼是否一致。
關(guān)鍵代碼:
Dao層@Update("UPDATE student set stdpassword = #{stdpassword} where stdid = #{stdid}")public int updatestudent(Student student);Controller層@RequestMapping("/pass")public String pass() {return "studentpass";}@RequestMapping("/topass")public String topass(String stdpassword, HttpSession session) throws IOException {Student stu = (Student) session.getAttribute("stulogin");String stdid = stu.getStdid();System.out.println(stdid + " " + stdpassword);Student studd = new Student();studd.setStdid(stdid);studd.setStdpassword(stdpassword);studentService.updateStudent(studd);return "redirect:tologin";}?
圖6-7管理員修改界面
5.3.8選課查詢界面
點擊選課查詢下拉框,請求映射到對應(yīng)的地址,通過映射到JSP界面后,JSP界面通過Ajax異步請求,訪問映射地址,通過通過自定義的Json類將數(shù)據(jù)通過Json數(shù)據(jù)格式返回到JSP界面,以leyui渲染的數(shù)據(jù)表格形式展現(xiàn)出來。用戶可以對查出來的課設(shè)信息進(jìn)行選擇,填寫同組成員信息后,將該課程設(shè)計加入到自己的課設(shè)中心。系統(tǒng)還會判斷收入的組號是不是已經(jīng)存在,存在則返回改界面。
主要代碼:
Dao層:@Select("select * from quesion limit #{page},#{size}")public List<Quesion> FindQuesionByPage(Map<String, Object> params);//分頁查詢用戶@Select("select count(gruid) from ?`group` where gruid =#{gruid}")public int findGroupExist(String gruid);@Insert("insert into `group`(gruid, grubody, gruhead) VALUES (#{gruid},#{grubody},#{gruhead})")public int addgroup(Group group);@Insert("insert into choque(stdid, queid, gruid) VALUES (#{stdid},#{queid},#{gruid})")public int addchoque(Choque choque);@Select("SELECT COUNT(*) FROM quesion")public int count() throws SQLException;//查詢用戶總數(shù)Controller層:@RequestMapping(value = "/findquesionPage", method = RequestMethod.GET)public void findStudentPage(Quesion quesion, HttpServletRequest request, HttpServletResponse response) throws Exception {request.setCharacterEncoding("utf-8");response.setContentType("application/json;charset=UTF-8");//設(shè)置編碼,防止json中文亂碼int index = 1;String sindex = request.getParameter("page");if (sindex != null && !"".equals(sindex)) {index = Integer.parseInt(sindex);}int size = 5;String ssize = request.getParameter("limit");if (ssize != null && !"".equals(ssize)) {size = Integer.parseInt(ssize);}PageBean<Quesion> pageBean = new PageBean<Quesion>();pageBean.setLimit(size);pageBean.setPage(index);System.out.println(pageBean.getLimit());try {pageBean = studentService.FindQuesionByPage(index, size);} catch (IOException | SQLException e) {e.printStackTrace();}JsonResult<Quesion> jr = new JsonResult<Quesion>("0", "", String.valueOf(pageBean.getTotalCount()), pageBean.getList());//交互json格式所需的對象String jsonStr = JSON.toJSONString(jr);//將JsonResult對象轉(zhuǎn)換為json串PrintWriter out = response.getWriter();out.print(jsonStr);out.close();}@RequestMapping("/toaddcourse")public String addcoursejsp(String queid, Model model, HttpSession session) throws IOException {Student stud = (Student) session.getAttribute("stulogin");model.addAttribute("stqueid", queid);model.addAttribute("ststdid", stud.getStdid());return "addcourses";}@RequestMapping("/addinfo")public String addgroup(Model model, HttpServletRequest request, Group group, HttpSession session) throws IOException {String stdid = request.getParameter("stdid");String queid = request.getParameter("queid");System.out.println(stdid +" " + queid);if (!studentService.findGroupExist(group.getGruid())) {Student stud = (Student) session.getAttribute("stulogin");model.addAttribute("stqueid", queid);model.addAttribute("ststdid", stud.getStdid());request.setAttribute("gruid", "該組號已經(jīng)存在");request.setAttribute("grubody", group.getGrubody());request.setAttribute("gruhead", group.getGruhead());return "addcourses";} else {Choque choque = new Choque();choque.setStdid(stdid);choque.setGruid(group.getGruid());choque.setQueid(queid);studentService.addgroup(group);studentService.addchoque(choque);return "tips";}}?
圖6-8-1?學(xué)生查看課設(shè)信息界面
?
圖6-8-2學(xué)生填寫同組信息添加課設(shè)界面
5.3.9成績查詢界面
點擊成績查詢下拉框,請求映射到對應(yīng)的地址,獲取session中的用戶id后,調(diào)用service層的函數(shù),在調(diào)用dao層的函數(shù),通過數(shù)據(jù)庫查到的信息,封裝在一個list集合中返回到j(luò)sp界面。
主要代碼:
Dao層:@Select("SELECT quesion.queid,quesion.quename,teaname,score from quesion,teacher,choque \n" +"where quesion.queid=choque.queid and quesion.teaid = teacher.teaid and choque.stdid = #{stdid}")public List<CourseInfo> findCourseInfo(String stdid);Controller層:@RequestMapping("dcourse")public?String?stdcourse(HttpSession?session,?Model?model)?throws?IOException?{Student?stu?=?(Student)?session.getAttribute("stulogin");List<CourseInfo>?courseInfos?=?studentService.findCourseInfo(stu.getStdid());model.addAttribute("courseinfos",?courseInfos);return?"stdcourse";}圖6-9成績查詢界面
?
5.3.10文件上傳操作
用戶點擊文件上傳按鈕后,請求映射到對應(yīng)的地址,通過異步Ajax請求,通過session中的id值,調(diào)用service層和dao層函數(shù),查詢該用戶上傳的全部文件,封賬成一個list集合,最后封裝成Map映射,返回的jsp界面,通過layui table對于表格的渲染,展示數(shù)據(jù)。
主要代碼:
Dao層:@Select("SELECT * FROM `files` where files.stdid= #{stdid} order by files.stdid limit #{begin}, #{offset}")public List<UserFile> queryByUserId (@Param("stdid") String stdid , @Param("begin") Integer begin , @Param("offset") Integer offset);@Select("SELECT COUNT(files.stdid) FROM files where files.stdid = #{stdid}")public int queryFileCounts(String stdid);@Insert("insert into files(filename, ext, path, size, type, downcount, uploadtime, stdid)\n" +" VALUES (#{filename},#{ext},#{path},#{size},#{type},#{downcount},#{uploadtime},#{stdid});")public ?void savefile(UserFile userFile);@Delete(" delete from files where fileid=#{fileid}")public void delete(Integer fileid);@Select("select * FROM files where fileid =#{fileid}")public UserFile queryByUserFileId(Integer fileid);@Update("update files set downcount = #{downcount} where fileid = #{fileid}")public int updateflie(UserFile userFile);Controller層:// 展示所有的文件@GetMapping("/index")public String fileIndex() {return "filelist";}@PostMapping("showfiles")@ResponseBodypublic Map<String, Object> queryAllFile(HttpSession session, HttpServletRequest request) {int page = Integer.parseInt(request.getParameter("page"));int limit = Integer.parseInt(request.getParameter("limit"));Student student = (Student) session.getAttribute("stulogin");List<UserFile> userFiles = userFileService.queryByUserId(student.getStdid(), page, limit);Map<String, Object> res = new HashMap<>();res.put("code", 0);res.put("count", userFileService.queryFileCounts(student.getStdid()));res.put("data", userFiles);return res;}// 上傳文件到數(shù)據(jù)庫@PostMapping("upload")@ResponseBodypublic Map<String, String> uploadFile(@RequestParam("file") MultipartFile file, HttpSession session) {System.out.println("uploadFile trigger");Map<String, String> res = new HashMap<>();try {// 獲取上傳用戶的IdStudent student = (Student) session.getAttribute("stulogin");// 獲取文件的原始名稱String fileName = file.getOriginalFilename();// 獲取文件的后綴String extension = FilenameUtils.getExtension(file.getOriginalFilename());// 獲取文件的大小long size = file.getSize();// 獲取文件的類型String type = file.getContentType();// 根據(jù)日期動態(tài)的生成目錄String localContainer = "/fileContainer";String uploadPath = ResourceUtils.getURL("classpath").getPath() + localContainer;String dateFormat = new SimpleDateFormat("yyyy-MM-dd").format(new Date());File dateDirPath = new File(uploadPath + File.separator + dateFormat);if (!dateDirPath.exists()) {dateDirPath.mkdirs();}// 處理文件上傳file.transferTo(new File(dateDirPath, fileName));// 將文件信息存入數(shù)據(jù)庫中UserFile userFile = new UserFile();userFile.setFilename(fileName);userFile.setExt('.' + extension);userFile.setPath(Paths.get(localContainer, dateFormat, fileName).toString());userFile.setSize(size);userFile.setType(type);userFile.setStdid(student.getStdid());userFileService.save(userFile);res.put("code", "0");res.put("msg", "上傳成功");res.put("url", "index");} catch (IOException e) {e.printStackTrace();res.put("code", "-1");res.put("msg", "上傳失敗");res.put("url", "index");}return res;}@GetMapping("delete")@ResponseBodypublic Map<String, Object> delete(Integer fileid) {//@PathVariable:接收請求路徑中占位符的值Map<String, Object> map = new HashMap<>();try {UserFile fileInfo = userFileService.queryByUserFileId(fileid);final String realPath = ResourceUtils.getURL("classpath").getPath() + fileInfo.getPath();File file = new File(realPath);if (file.exists()) {file.delete(); ?//立即刪除}userFileService.delete(fileid);map.put("code", 0);map.put("msg", "刪除成功!");} catch (FileNotFoundException e) {e.printStackTrace();map.put("code", -1);map.put("msg", "刪除成功!");}return map;}public void getFile(String openStyle, Integer fileid, HttpServletResponse response) throws IOException {UserFile file = userFileService.queryByUserFileId(fileid);if(openStyle.equals("inline")){// 獲取文件信息final String realPath = ResourceUtils.getURL("classpath").getPath() + file.getPath();// 獲取文件輸入流FileInputStream is = new FileInputStream(new File(realPath));// 附件下載response.setHeader("content-disposition", openStyle+";filename=" + URLEncoder.encode(file.getFilename(), "UTF-8"));// 獲取響應(yīng)response輸出流ServletOutputStream os = response.getOutputStream();// 文件拷貝IOUtils.copy(is, os);IOUtils.closeQuietly(is);IOUtils.closeQuietly(os);}// 更新下載次數(shù)if(openStyle.equals("attachment")){file.setDowncount(file.getDowncount() + 1);userFileService.update(file);}}@GetMapping("preview")public void preview(Integer fileid, HttpServletResponse response) throws IOException {String openStyle = "inline";getFile(openStyle,fileid,response);}@GetMapping("download")public void download( Integer fileid, HttpServletResponse response){String openStyle = "attachment";try{getFile(openStyle,fileid,response);} catch (IOException e) {e.printStackTrace();}}?
圖6-10-1文件界面
?
圖6-10-2文件上傳界面
?
圖6-10-3文件預(yù)覽界面
5.3.10教師端信息查詢
老師點擊信息查詢后,在session中獲取老師的信息,傳入controller層中,調(diào)用service和dao層函數(shù),將得到的結(jié)果以封裝到list集合,將集合和一些其他信息一同封裝到map集合中,通過json串的形式一同傳回jsp界面,通過leyui table表格渲染展示。教師也可以對選擇了該課設(shè)的學(xué)生給出成績,也可以修改成績,方法類似,這里不再贅述。
主要代碼:
Dao層:@Select("SELECT student.stdid,student.stdname,quesion.queid,quesion.quename,choque.score\n" +"FROM student,choque,quesion\n" +"WHERE student.stdid = choque.stdid AND choque.queid = quesion.queid AND quesion.teaid = #{teaid}")public List<Studentlist> showstudent(String teaid);@Update("UPDATE choque SET score = #{score} WHERE stdid = #{stdid} AND queid = #{queid}")public void upscore(Studentlist studentlist);Controller層:@RequestMapping("owstudents")@ResponseBodypublic?Map<String,?Object>?showstudent(HttpSession?session,?Model?model)?throws?IOException?{Teacher?teacher?=?(Teacher)?session.getAttribute("tealogin");List<Studentlist>?studentlists?=?teacherService.showstudent(teacher.getTeaid());Map<String,?Object>?res?=?new?HashMap<>();res.put("code",?0);res.put("data",?studentlists);return?res;?}@RequestMapping("/addscore")@ResponseBodypublic?Map<String,?Object>?addscore(HttpSession?session,?Studentlist?studentlist)?throws?IOException?{teacherService.addscore(studentlist);Map<String,?Object>?res?=?new?HashMap<>();res.put("code",?0);res.put("msg",?"添加成功!");return?res;}?
圖6-11教師端信息查詢界面
?
圖6-12教師端條件查詢界面
5.3.11攔截器操作
登錄攔截器,默認(rèn)攔截/student 路徑下的所有請求,若session中存在student,則放行,如不存在則跳轉(zhuǎn)到登錄界面。
public?class?LoginConfig?implements?WebMvcConfigurer?{@Overridepublic?void?addInterceptors(InterceptorRegistry?registry)?{//注冊TestInterceptor攔截器InterceptorRegistration?registration?=?registry.addInterceptor(new?LoginInterceptor());registration.addPathPatterns("/student/*");registration.excludePathPatterns("udent/tologin","udent/register","udent/registerStudent","udent/login","udent/code","/css/**","/images/**","/imagess/**","/js/**","/lib/**"?);}}@Override public?boolean?preHandle(HttpServletRequest?request,?HttpServletResponse?response,?Object?handler)?throws?Exception?{System.out.println("====================攔截前================="); HttpSession?session?=?request.getSession(); Student?user?=?(Student)?session.getAttribute("stulogin"); if?(user?!=?null)?{ System.out.println("====================用戶已登錄================="+user.toString()); return?true;} if?(!request.getContextPath().equals(request.getContextPath()?+?"student/tologin"))?{response.sendRedirect(request.getContextPath()?+?"udent/tologin");}return?false;項目完整源碼+說明書+軟件說明+數(shù)據(jù)庫表設(shè)計?:
點他:🍞正在為您運送作品詳情
總結(jié)
以上是生活随笔為你收集整理的SpringBoot + Servlet + Mybatis+ layui 学生选课管理系统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习--岭回归10
- 下一篇: 耳机在macOS系统电脑上怎么听不到任何