视频教程: 01-创建数据库_哔哩哔哩_bilibili
源码下载:百度网盘 请输入提取码
准备工作
- 创建数据库
- 配置IDEA后端
- 导入前端
前言:
把代码掰开写进博客里,主要是让自己在整理笔记的过程中,多去思考完成这个功能的核心代码部分是什么。一个功能一个功能地写代码、测试,如果遇到bug,再退回检查新写的这部分代码,测试成功再进一步。之前没做笔记跟着视频教程写项目时,写着写着就迷糊了,乱了,一遇到bug就全盘崩溃。所以,在此记录写项目的完整过程,留下清晰的足迹。学习的方法仍然在不断地改进,以求找到最适合自己的。
过程中的代码名称可能有点变化,以最终的源码文件为准
学生登录
LoginController
package com.study.controller;import com.study.pojo.Users;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;import javax.servlet.http.HttpServletRequest;@Controller
public class LoginController {//自动注入@AutowiredUsersService usersService;//老师登录和学生登录的互相跳转@GetMapping("toTeacher")public String toTeaLogin(){return "teacher/login";}@GetMapping("toStuLogin")public String toStuLogin(){return "student/login";}//学生登录@PostMapping("/stulogin")public String stulogin(String username, String userpwd, HttpServletRequest servletRequest){Users login = usersService.login(username, userpwd);if(login==null){System.out.println("登录失败");servletRequest.getSession().setAttribute("mag","密码错误");return "redirect:/toStuLogin";}else{System.out.println("登录成功");return "student/StuMan";}}
}
UserDao
package com.study.dao;import com.study.pojo.Users;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface UsersDao {Users login(String username,String userpwd);
}
Users
package com.study.pojo;public class Users {private Integer userid;private String username;private String userpwd;private String truename;private Integer classid;public Integer getUserid() {return userid;}public void setUserid(Integer userid) {this.userid = userid;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getUserpwd() {return userpwd;}public void setUserpwd(String userpwd) {this.userpwd = userpwd;}public String getTruename() {return truename;}public void setTruename(String truename) {this.truename = truename;}public Integer getClassid() {return classid;}public void setClassid(Integer classid) {this.classid = classid;}
}
UsersServiceImpl
package com.study.service.impl;import com.study.dao.UsersDao;
import com.study.pojo.Users;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UsersServiceImpl implements UsersService {@AutowiredUsersDao usersDao;@Overridepublic Users login(String username, String userpwd) {Users login=usersDao.login(username,userpwd);return login;}
}
UsersService
package com.study.service;import com.study.pojo.Users;public interface UsersService {Users login(String username,String userpwd);
}
UsersMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTO Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.study.dao.UsersDao"><select id="login" resultType="com.study.pojo.Users">select * from users where username=#{username}and userpwd=#{userpwd}</select>
</mapper>
application.properties
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/exam?useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.druid.username=root
spring.datasource.druid.password=admin#其他配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=60000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000#mapper.xml配置
mybatis.mapper-locations=classpath:mapper/*.xml
运行结果:
- 在exam数据库的user表中,手动添加一条数据用于测试
- 在浏览器输入地址访问:localhost:8080/
- 主要实现的功能:
- 登录成功,跳转页面
- 登录失败,报错”密码错误“
学生注册
StuUserController
package com.study.controller;import com.study.pojo.PClass;
import com.study.pojo.Users;
import com.study.service.PClassService;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;import javax.servlet.http.HttpServletRequest;
import java.util.List;/*** 学生的controller层*/
@Controller
public class StuUserController {@AutowiredUsersService usersService;@AutowiredPClassService pClassService;@GetMapping("/register")public String toRegister(Model model){//调用班级的service层,获取全部班级信息List<PClass> allPClass = pClassService.getAllPClass();//把班级列表传回前台model.addAttribute("list",allPClass);return "student/register";}@PostMapping("/CanRegister")public String register(Users users, String username, String userpwd, String truename, Integer classid, HttpServletRequest request){Users byName=usersService.getByName(username);users.setUsername(username);users.setUserpwd(userpwd);users.setTruename(truename);users.setClassid(classid);usersService.addUsers(users);//判断用户是否存在,byName为null时,用户不存在if(byName==null){if(userpwd.equals("")){request.getSession().setAttribute("msg1","密码为空");//在前端显示return "redirect:/register";//如果密码为空,返回注册页面}if(truename.equals("")){request.getSession().setAttribute("msg2","真实姓名为空");//在前端显示return "redirect:/register";//如果真实姓名为空,返回注册页面}if(username.equals("")){request.getSession().setAttribute("msg3","用户名为空");//在前端显示return "redirect:/register";//如果用户名为空,返回注册页面}System.out.println("用户可以注册");return "redirect:/stulogin";//返回登录页面}else{System.out.println("用户已存在");return "redirect:/register";//返回注册页面}}
}
PClassDao
package com.study.dao;import com.study.pojo.PClass;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;import java.util.List;/*** 接口层*/
@Mapper
@Component
public interface PClassDao {//查询班级的所有信息List<PClass> getAllClass();
}
UsersDao
package com.study.dao;import com.study.pojo.Users;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface UsersDao {//由此去编写UsersDao.xml文件里的sql语句//注册Integer addUsers(Users users);//查询用户的接口Users getByName(String username);
}
PClass
package com.study.pojo;/*** 班级的实体类* 写完实体类,就去写dao接口,再写sql语句*/
public class PClass {private Integer classid;private String classname;public Integer getClassid() {return classid;}public void setClassid(Integer classid) {this.classid = classid;}public String getClassname() {return classname;}public void setClassname(String classname) {this.classname = classname;}
}
UsersService
package com.study.service;import com.study.pojo.Users;public interface UsersService {//注册Integer addUsers(Users users);//查询用户Users getByName(String username);
}
UsersServiceImpl
package com.study.service.impl;import com.study.dao.UsersDao;
import com.study.pojo.Users;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UsersServiceImpl implements UsersService {@Overridepublic Integer addUsers(Users users){return usersDao.addUsers(users);}@Overridepublic Users getByName(String username) {return usersDao.getByName(username);}
}
PClassService
package com.study.service;import com.study.pojo.PClass;import java.util.List;public interface PClassService {List<PClass> getAllPClass();
}
PClassServiceImpl
package com.study.service.impl;import com.study.dao.PClassDao;
import com.study.pojo.PClass;
import com.study.service.PClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class PClassServiceImpl implements PClassService {@AutowiredPClassDao pClassDao;@Overridepublic List<PClass> getAllPClass() {return pClassDao.getAllClass();}
}
UsersMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTO Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.study.dao.UsersDao"><insert id="addUsers">insert into users(userid,username,userpwd,truename,classid)values(default,#{username},#{userpwd},#{truename},#{classid})</insert><select id="getByName" resultType="com.study.pojo.Users">select * from users where username=#{username}</select>
</mapper>
PClassMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.study.dao.PClassDao"><select id="getAllClass" resultType="com.study.pojo.PClass">select * from pclass</select>
</mapper>
老师登录
总体思路:
- 编写TeaUser实体类
- 编写TeaUserDao接口
- 编写SQL语句,在TeaUserMapper.xml文件里,与接口直接映射
- 编写TeaUserService接口
- 编写TeaUserServiceImpl
LoginController
package com.study.controller;import com.study.pojo.TeaUser;
import com.study.pojo.Users;
import com.study.service.TeaUserService;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;import javax.servlet.http.HttpServletRequest;@Controller
public class LoginController {//教师登录@PostMapping("/tealogin")public String teaLogin(String username,String userpwd,HttpServletRequest request){TeaUser teaUser = teaUserService.teaLogin(username, userpwd);if(teaUser==null){//登录密码错误request.getSession().setAttribute("msg","登录密码错误");return "redirect:/toTeacher";//返回登录首页}else{//登录成功String teaTruename = teaUserService.getTeaTruename(username);//获取老师的姓名request.getSession().setAttribute("Teatruename",teaTruename);//把老师的姓名返回前端页面return "teacher/manage";//跳转到教师的管理页面}}
}
TeaUserDao
package com.study.dao;import com.study.pojo.TeaUser;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;@Mapper
@Component
public interface TeaUserDao {//教师登录TeaUser teaLogin(String username,String userpwd);//获取教师姓名String getTeaTruename(String username);
}
TeaUser
package com.study.pojo;public class TeaUser {private Integer userid;private String username;private String userpwd;private String truename;private String classid;public Integer getUserid() {return userid;}public void setUserid(Integer userid) {this.userid = userid;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getUserpwd() {return userpwd;}public void setUserpwd(String userpwd) {this.userpwd = userpwd;}public String getTruename() {return truename;}public void setTruename(String truename) {this.truename = truename;}public String getClassid() {return classid;}public void setClassid(String classid) {this.classid = classid;}
}
TeaUserService
package com.study.service;import com.study.pojo.TeaUser;public interface TeaUserService {TeaUser teaLogin(String username,String userpwd);String getTeaTruename(String username);
}
TeaUserServiceImpl
package com.study.service.impl;import com.study.dao.TeaUserDao;
import com.study.pojo.TeaUser;
import com.study.service.TeaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class TeaUserServiceImpl implements TeaUserService {@AutowiredTeaUserDao teaUserDao;//自动注入@Overridepublic TeaUser teaLogin(String username, String userpwd) {return teaUserDao.teaLogin(username,userpwd);}@Overridepublic String getTeaTruename(String username) {return teaUserDao.getTeaTruename(username);}
}
TeaUserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.study.dao.TeaUserDao"><select id="teaLogin" resultType="com.study.pojo.TeaUser">select * from teauser where username=#{username}and userpwd=#{userpwd}</select><select id="getTeaTruename" resultType="java.lang.String">select truename from teauser where username=#{username}</select>
</mapper>
运行结果:
学生的查询操作
LoginController
package com.study.controller;import com.study.pojo.TeaUser;
import com.study.pojo.Users;
import com.study.service.TeaUserService;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;import javax.servlet.http.HttpServletRequest;@Controller
public class LoginController {//教师登录@PostMapping("/tealogin")public String teaLogin(String username,String userpwd,HttpServletRequest request){//查询班级idInteger classId=teaUserService.getClassId(username);//老师登录TeaUser teaUser = teaUserService.teaLogin(username, userpwd);if(teaUser==null){//登录密码错误request.getSession().setAttribute("msg","登录密码错误");return "redirect:/toTeacher";//返回登录首页}else{//登录成功String teaTruename = teaUserService.getTeaTruename(username);//获取老师的姓名request.getSession().setAttribute("Teatruename",teaTruename);//把老师的姓名返回前端页面request.getSession().setAttribute("TeaClassid",classId);//把班级id返回前端return "teacher/manage";//跳转到教师的管理页面}}
}
TeacherController
package com.study.controller;import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.pojo.PClass;
import com.study.pojo.Users;
import com.study.service.PClassService;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpServletRequest;
import java.util.List;@Controller
public class TeacherController {@AutowiredPClassService pClassService;@AutowiredUsersService usersService;@GetMapping("/StudentList")public String findStuUsers(HttpServletRequest request, Model model,@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum){//获取班级idInteger teaClassid = (Integer)request.getSession().getAttribute("TeaClassid");//查看老师所在班级PClass classid = pClassService.getClassid(teaClassid);model.addAttribute("pj",classid);//返回数据到前端//设置分页PageHelper.startPage(pageNum,5);//查询学生列表List<Users> users = usersService.pageByClassid(teaClassid);PageInfo<Users> usersPageInfo = new PageInfo<>(users);model.addAttribute("pageInfo",usersPageInfo);model.addAttribute("liss",users);return "teacher/StudentList";}
}
PClassDao
package com.study.dao;import com.study.pojo.PClass;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;import java.util.List;/*** 接口层*/
@Mapper
@Component
public interface PClassDao {//查询班级idPClass getClassId(Integer classid);
}
TeaUserDao
package com.study.dao;import com.study.pojo.TeaUser;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;@Mapper
@Component
public interface TeaUserDao {//获取班级idInteger getClassId(String username);
}
UserDao
package com.study.dao;import com.study.pojo.Users;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface UsersDao {List<Users> pageById(Integer classid);
}
PClassService
package com.study.service;import com.study.pojo.PClass;import java.util.List;public interface PClassService {PClass getClassid(Integer classid);
}
TeaUserService
package com.study.service;import com.study.pojo.TeaUser;public interface TeaUserService {Integer getClassId(String username);
}
UsersService
package com.study.service;import com.study.pojo.Users;import java.util.List;public interface UsersService {//根据id查询List<Users> pageByClassid(Integer classid);
}
PClassServiceImpl
package com.study.service.impl;import com.study.dao.PClassDao;
import com.study.pojo.PClass;
import com.study.service.PClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class PClassServiceImpl implements PClassService {@Overridepublic PClass getClassid(Integer classid) {return pClassDao.getClassId(classid);}
}
TeaUserServiceImpl
package com.study.service.impl;import com.study.dao.TeaUserDao;
import com.study.pojo.TeaUser;
import com.study.service.TeaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class TeaUserServiceImpl implements TeaUserService {@Overridepublic Integer getClassId(String username) {return teaUserDao.getClassId(username);}
}
UsersServiceImpl
package com.study.service.impl;import com.study.dao.UsersDao;
import com.study.pojo.Users;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UsersServiceImpl implements UsersService {@Overridepublic List<Users> pageByClassid(Integer classid) {return usersDao.pageById(classid);}
}
PClassMapper.xml
<select id="getClassId" resultType="com.study.pojo.PClass">select * from pclass where classid=#{classid}
</select>
TeaUserMapper.xml
<select id="getClassId" resultType="java.lang.Integer">select classid from teauser where username=#{username}
</select>
UsersMapper.xml
<select id="pageById" resultType="com.study.pojo.Users">select * from users where classid=#{classid}
</select>
运行结果:
学生的添加操作
TeacherController
package com.study.controller;import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.pojo.PClass;
import com.study.pojo.Users;
import com.study.service.PClassService;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpServletRequest;
import java.util.List;@Controller
public class TeacherController {@AutowiredPClassService pClassService;@AutowiredUsersService usersService;@GetMapping("/StudentList")public String findStuUsers(HttpServletRequest request, Model model,@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum){//获取所有班级信息List<PClass> allPClass=pClassService.getAllPClass();model.addAttribute("list",allPClass);return "teacher/StudentList";}//添加学生@PostMapping("/addStu")public String addStuUsers(String username,String userpwd,String truename,Integer classid){//查询学生是否存在Users byName = usersService.getByName(username);Users users=new Users();if(byName==null){users.setUsername(username);users.setUserpwd(userpwd);users.setTruename(truename);users.setClassid(classid);usersService.addUsers(users);}else{System.out.println("该学生已存在");}return "rediect:/StudentList";}
}
运行结果:
学生的修改操作
TeacherController
package com.study.controller;import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.pojo.PClass;
import com.study.pojo.Users;
import com.study.service.PClassService;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest;
import java.util.List;/*** exams com.sxt.controller* 2022-07-03 09:43** @author sxt Administrator* description : 不可描述* @date 2022/7/3 9:43*/
@Controller
public class TeacherController {//修改学生@PostMapping("/updateStu")public String updStuUser(Users users){usersService.updateUsers(users);return "redirect:/StudentList";}}
UserDao
package com.study.dao;import com.study.pojo.Users;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;import java.util.List;@Mapper
@Component
public interface UsersDao {//由此去编写UsersDao.xml文件里的sql语句Integer updateUsers(Users users);Users getByUserid(Integer userid);
}
UsersService
package com.study.service;import com.study.pojo.Users;import java.util.List;public interface UsersService {Integer updateUsers(Users users);Users getByUserid(Integer userid);
}
UsersServiceImpl
package com.study.service.impl;import com.study.dao.UsersDao;
import com.study.pojo.Users;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UsersServiceImpl implements UsersService {@Overridepublic Integer updateUsers(Users users) {//修改学生信息return usersDao.updateUsers(users);}@Overridepublic Users getByUserid(Integer userid) {return usersDao.getByUserid(userid);}
}
UsersMapper.xml
<update id="updateUsers">update users set username=#{username},userpwd=#{userpwd},truename=#{truename},classid=#{classid}where userid=#{userid}
</update>
<select id="getByUserid" resultType="com.study.pojo.Users">select * from users where userid=#{userid}
</select>
学生的删除操作
写代码的逻辑顺序:
- controller层:TeacherController
- dao层:UsersDao
- mapper层:UsersMapper.xml
- service层:UsersService
- service层:UsersServiceImpl
- controller层:TeacherController
TeacherController
package com.study.controller;import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.pojo.PClass;
import com.study.pojo.Users;
import com.study.service.PClassService;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest;
import java.util.List;/*** exams com.sxt.controller* 2022-07-03 09:43** @author sxt Administrator* description : 不可描述* @date 2022/7/3 9:43*/
@Controller
public class TeacherController {//删除单个学生@GetMapping("/DeleteStu")public String deleteUsers(HttpServletRequest request){Integer userid=Integer.valueOf(request.getParameter("userid"));//获取学生id,通过id单个删除学生usersService.delUserid(userid);return "redirect:/StudentList";}//删除老师所在班级的所有学生@GetMapping("/deleteAll")public String deleteUsersAll(HttpServletRequest request){//获取班级idInteger teaClassid = (Integer) request.getSession().getAttribute("TeaClassid");usersService.delClassid(teaClassid);return "redirect:/StudentList";}}
UserDao
package com.study.dao;import com.study.pojo.Users;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;import java.util.List;@Mapper
@Component
public interface UsersDao {Integer delUserid(Integer userid);Integer delClassid(Integer classid);
}
UsersMapper.xml
<delete id="delUserid">delete from users where userid=#{userid}</delete><delete id="delClassid">delete from users where classid=#{classid}</delete>
UsersService
package com.study.service;import com.study.pojo.Users;import java.util.List;public interface UsersService {//根据id删除学生Integer delUserid(Integer userid);//根据班级id删除整班学生Integer delClassid(Integer classid);
}
UsersServiceImpl
package com.study.service.impl;import com.study.dao.UsersDao;
import com.study.pojo.Users;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UsersServiceImpl implements UsersService {@Overridepublic Integer delUserid(Integer userid) {return usersDao.delUserid(userid);}@Overridepublic Integer delClassid(Integer classid) {return usersDao.delClassid(classid);}
}
运行结果:
题的查询
Subject
package com.study.pojo;public class Subject {private Integer sid;private Integer cno;private String scontent;private String sa;private String sb;private String sc;private String sd;private String skey;private Course course;public String getSkey() {return skey;}public void setSkey(String skey) {this.skey = skey;}public Course getCourse() {return course;}public void setCourse(Course course) {this.course = course;}public Integer getSid() {return sid;}public void setSid(Integer sid) {this.sid = sid;}public Integer getCno() {return cno;}public void setCno(Integer cno) {this.cno = cno;}public String getScontent() {return scontent;}public void setScontent(String scontent) {this.scontent = scontent;}public String getSa() {return sa;}public void setSa(String sa) {this.sa = sa;}public String getSb() {return sb;}public void setSb(String sb) {this.sb = sb;}public String getSc() {return sc;}public void setSc(String sc) {this.sc = sc;}public String getSd() {return sd;}public void setSd(String sd) {this.sd = sd;}
}
Course
package com.study.pojo;public class Course {private Integer cno;private String cname;public Integer getCno() {return cno;}public void setCno(Integer cno) {this.cno = cno;}public String getCname() {return cname;}public void setCname(String cname) {this.cname = cname;}
}
SubjectDao
package com.study.dao;import com.study.pojo.Subject;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;import java.util.List;@Mapper
@Component
public interface SubjectDao {//查询全部题List<Subject> getAllSubject();
}
CourseDao
package com.study.dao;import com.study.pojo.Course;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;@Mapper
@Component
public interface CourseDao {//通过id查询课程Course getAllById(Integer cno);
}
SubjectMapper.xml
<select id="getAllSubject" resultType="com.study.pojo.Subject">select * from subject</select>
CourseMapper.xml
<select id="getAllById" resultType="com.study.pojo.Course">select * from course where cno=#{cno}</select>
SubjectService
package com.study.service;import com.study.pojo.Subject;import java.util.List;public interface SubjectService {List<Subject> getAllSubject();
}
CourseService
package com.study.service;import com.study.pojo.Course;
import org.springframework.stereotype.Service;public interface CourseService {Course getAllById(Integer cno);
}
SubjectServiceImpl
package com.study.service.impl;import com.study.dao.SubjectDao;
import com.study.pojo.Subject;
import com.study.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class SubjectServiceImpl implements SubjectService {@AutowiredSubjectDao subjectDao;@Overridepublic List<Subject> getAllSubject() {return subjectDao.getAllSubject();}
}
CourseServiceImpl
package com.study.service.impl;import com.study.dao.CourseDao;
import com.study.pojo.Course;
import com.study.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class CourseServiceImpl implements CourseService {@AutowiredCourseDao courseDao;@Overridepublic Course getAllById(Integer cno) {return courseDao.getAllById(cno);}
}
TeacherController
package com.study.controller;import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.pojo.Course;
import com.study.pojo.PClass;
import com.study.pojo.Subject;
import com.study.pojo.Users;
import com.study.service.CourseService;
import com.study.service.PClassService;
import com.study.service.SubjectService;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest;
import java.util.List;/*** exams com.sxt.controller* 2022-07-03 09:43** @author sxt Administrator* description : 不可描述* @date 2022/7/3 9:43*/
@Controller
public class TeacherController {//查询题目@GetMapping("/finddanxuan")public String findSingle(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,Model model){PageHelper.startPage(pageNum,5);//题目分页显示List<Subject> allSubject = subjectService.getAllSubject();for(Subject subject:allSubject){//查询题目Course allById=courseService.getAllById(subject.getCno());subject.setCourse(allById);}PageInfo<Subject> subjectPageInfo = new PageInfo<>(allSubject);model.addAttribute("pageInfo",subjectPageInfo);//传回前端 model.addAttribute("subjectlist",allSubject);//传回前端 return "teacher/Single";}
}
运行结果:
题的添加
course这块在这部分主要是做下拉框的课程选择部分的
SubjectDao
package com.study.dao;import com.study.pojo.Subject;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;import java.util.List;@Mapper
@Component
public interface SubjectDao {//添加题Integer addSubject(Subject subject);
}
CourseDao
package com.study.dao;import com.study.pojo.Course;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;import java.util.List;@Mapper
@Component
public interface CourseDao {//查询全部List<Course> getAllCourse();
}
SubjectMapper.xml
<insert id="addSubject">insert into subject(sid,cno,scontent,sa,sb,sc,sd,skey)values(default,#{cno},#{scontent},#{sa},#{sb},#{sc},#{sd},#{skey})</insert>
CourseMapper.xml
<select id="getAllCourse" resultType="com.study.pojo.Course">select * from course</select>
SubjectService
package com.study.service;import com.study.pojo.Subject;import java.util.List;public interface SubjectService {Integer addSingle(Subject subject);
}
CourseService
package com.study.service;import com.study.pojo.Course;
import org.springframework.stereotype.Service;import java.util.List;public interface CourseService {List<Course> getAllCourse();
}
SubjectServiceImpl
package com.study.service.impl;import com.study.dao.SubjectDao;
import com.study.pojo.Subject;
import com.study.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class SubjectServiceImpl implements SubjectService {@Overridepublic Integer addSingle(Subject subject) {return subjectDao.addSubject(subject);}
}
CourseServiceImpl
package com.study.service.impl;import com.study.dao.CourseDao;
import com.study.pojo.Course;
import com.study.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class CourseServiceImpl implements CourseService {@Overridepublic List<Course> getAllCourse() {return courseDao.getAllCourse();}
}
TeacherController
package com.study.controller;import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.pojo.Course;
import com.study.pojo.PClass;
import com.study.pojo.Subject;
import com.study.pojo.Users;
import com.study.service.CourseService;
import com.study.service.PClassService;
import com.study.service.SubjectService;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest;
import java.util.List;/*** exams com.sxt.controller* 2022-07-03 09:43** @author sxt Administrator* description : 不可描述* @date 2022/7/3 9:43*/
@Controller
public class TeacherController {//获取所有课程@ResponseBody //因为返回的是一组数据@GetMapping("/findAllCourse")public List<Course> courseList(){List<Course> allCourse = courseService.getAllCourse();return allCourse;}//添加题@PostMapping("/addSingle")public String addSingle(Integer cno,String scontent,String sa,String sb,String sc,String sd,String skey){Subject subject=new Subject();subject.setCno(cno);subject.setScontent(scontent);subject.setSa(sa);subject.setSb(sb);subject.setSc(sc);subject.setSd(sd);subject.setSkey(skey);subjectService.addSingle(subject);//添加题目return "redirect:/finddanxuan";}
}
运行结果:
题的修改
SubjectDao
package com.study.dao;import com.study.pojo.Subject;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;import java.util.List;@Mapper
@Component
public interface SubjectDao {//修改Integer updateSingle(Subject subject);//id查询Subject getBySid(Integer sid);
}
SubjectMapper.xml
<update id="updateSingle">update subject set cno=#{cno},scontent=#{scontent},sa=#{sa},sb=#{sb},sc=#{sc},sd=#{sd},skey=#{skey}where sid=#{sid}</update><select id="getBySid" resultType="com.study.pojo.Subject">select * from subject where sid=#{sid}</select>
SubjectService
package com.study.service;import com.study.pojo.Subject;import java.util.List;public interface SubjectService {Integer updateSingle(Subject subject);Subject getBySid(Integer sid);
}
SubjectServiceImpl
package com.study.service.impl;import com.study.dao.SubjectDao;
import com.study.pojo.Subject;
import com.study.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class SubjectServiceImpl implements SubjectService {@Overridepublic Integer updateSingle(Subject subject) {return subjectDao.updateSingle(subject);}@Overridepublic Subject getBySid(Integer sid) {return subjectDao.getBySid(sid);}
}
TeacherController
package com.study.controller;import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.pojo.Course;
import com.study.pojo.PClass;
import com.study.pojo.Subject;
import com.study.pojo.Users;
import com.study.service.CourseService;
import com.study.service.PClassService;
import com.study.service.SubjectService;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest;
import java.util.List;/*** exams com.sxt.controller* 2022-07-03 09:43** @author sxt Administrator* description : 不可描述* @date 2022/7/3 9:43*/
@Controller
public class TeacherController {//获取题数据@ResponseBody@PostMapping("/findBySid")public Subject findBySid(@RequestBody Subject subject){Subject subject1 = subjectService.getBySid(subject.getSid());if(subject1!=null){return subject1;}else{return null;}}//修改题@PostMapping("/updateSingle")public String updSingle(Subject subject){subjectService.updateSingle(subject);return "redirect:/finddanxuan";}
}
运行结果:
题的删除
SubjectDao
package com.study.dao;import com.study.pojo.Subject;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;import java.util.List;@Mapper
@Component
public interface SubjectDao {//删除题Integer delSingle(Integer sid);
}
SubjectMapper.xml
<delete id="delSingle">delete from subject where sid=#{sid}</delete>
SubjectService
package com.study.service;import com.study.pojo.Subject;import java.util.List;public interface SubjectService {Integer delSingle(Integer sid);
}
SubjectServiceImpl
package com.study.service.impl;import com.study.dao.SubjectDao;
import com.study.pojo.Subject;
import com.study.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class SubjectServiceImpl implements SubjectService {@Overridepublic Integer delSingle(Integer sid) {return subjectDao.delSingle(sid);}
}
TeacherController
package com.study.controller;import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.pojo.Course;
import com.study.pojo.PClass;
import com.study.pojo.Subject;
import com.study.pojo.Users;
import com.study.service.CourseService;
import com.study.service.PClassService;
import com.study.service.SubjectService;
import com.study.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest;
import java.util.List;/*** exams com.sxt.controller* 2022-07-03 09:43** @author sxt Administrator* description : 不可描述* @date 2022/7/3 9:43*/
@Controller
public class TeacherController {//删除题@PostMapping("/deleteSingle")public String delSingle(@RequestParam Integer sid){subjectService.delSingle(sid);return "redirect:/finddanxuan";}
}
运行结果:
考试信息操作
- 创建考试
- 查询考试
- 修改考试
- 删除考试
TeacherController
package com.sxt.controller;import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sxt.pojo.*;
import com.sxt.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.tags.Param;import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;/*** exams com.sxt.controller* 2022-07-03 09:43** @author sxt Administrator* description : 不可描述* @date 2022/7/3 9:43*/
@Controller
public class TeacherController {@AutowiredExamService examService;@AutowiredPaperService paperService;//查询试卷信息@GetMapping("/paperDetails")public String paperDetails(Integer eid,Model model){List<Paper> byEid = paperService.getByEid(eid);model.addAttribute("tm",byEid);Exam exam = examService.getExam(eid);model.addAttribute("exam",exam);return "teacher/paperDetails";}//获取所有班级,主要是下拉框里的内容@ResponseBody@GetMapping("/findAllClasses")public List<PClass> findAllClasses(){List<PClass> allPClass=pClassService.getAllPClass();return allPClass;}//查询一场考试@ResponseBody@PostMapping("/findByOneExam")public Exam findByOneExam(@RequestBody Exam exam){Exam exam1 = examService.getExam(exam.getEid());if(exam1==null){//考试信息为空,则返回空,否则返回考试信息return null;}else{return exam1;}}//修改考试信息@PostMapping("/updateExam")public String updateExam(Exam exam){examService.updExam(exam);return "redirect:/selectexam";}//删除考试信息@GetMapping("/deleteExam")public String deleteExam(Integer eid){examService.delExam(eid);return "redirect:/selectexam";}
}
ExamDao
package com.sxt.dao;import com.sxt.pojo.Exam;
import org.apache.ibatis.annotations.Mapper;import java.util.List;/*** exams com.sxt.dao* 2022-07-17 10:16** @author sxt Administrator* description : 不可描述* @date 2022/7/17 10:16*/
@Mapper
public interface ExamDao {Integer addExam(Exam exam);//查询所有考试信息List<Exam> getAllExam();//根据id查询Exam getExam(Integer eid);//修改考试Integer updExam(Exam exam);//删除考试Integer delExam(Integer eid);
}
PaperDao
package com.sxt.dao;import com.sxt.pojo.Paper;
import org.apache.ibatis.annotations.Mapper;import java.util.List;/*** exams com.sxt.dao* 2022-07-17 11:07** @author sxt Administrator* description : 不可描述* @date 2022/7/17 11:07*/
@Mapper
public interface PaperDao {Integer addPaper(Paper paper);//根据考试信息id查询试卷List<Paper> getByEid(Integer eid);//删除试卷信息:eidInteger deleteByEid(Integer eid);
}
Exam
package com.sxt.pojo;import java.util.Date;public class Exam {private Integer eid;private String pname;private Integer cno;private Integer useid;private Integer classid;private Integer singlenumber;private Integer singlecore;private Date examdate;private Date examtime;private Integer testtime;private Course course;//getter,setter方法略
}
Paper
package com.sxt.pojo;
public class Paper {private Integer pid;private Integer eid;private Integer sid;private Integer cno;private String scontent;private String sa;private String sb;private String sc;private String sd;private String skey;//getter,setter方法略
}
ExamService
package com.sxt.service;import com.sxt.pojo.Exam;import java.util.List;public interface ExamService {Integer addExam(Exam exam);List<Exam> getAllExam();Exam getExam(Integer eid);Integer updExam(Exam exam);Integer delExam(Integer eid);
}
PaperService
package com.sxt.service;import com.sxt.pojo.Paper;import java.util.List;public interface PaperService {Integer addPaper(Paper paper);List<Paper> getByEid(Integer eid);
}
ExamServiceImpl
package com.sxt.service.impl;import com.sxt.dao.ExamDao;
import com.sxt.dao.PaperDao;
import com.sxt.pojo.Exam;
import com.sxt.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class ExamServiceImpl implements ExamService {@AutowiredExamDao examDao;@AutowiredPaperDao paperDao;@Overridepublic Integer addExam(Exam exam) {return examDao.addExam(exam);}@Overridepublic List<Exam> getAllExam() {return examDao.getAllExam();}@Overridepublic Exam getExam(Integer eid) {return examDao.getExam(eid);}@Overridepublic Integer updExam(Exam exam) {return examDao.updExam(exam);}@Overridepublic Integer delExam(Integer eid) {paperDao.deleteByEid(eid);//清空试卷信息Integer integer = examDao.delExam(eid);return integer;}
}
PaperServiceImpl
package com.sxt.service.impl;import com.sxt.dao.PaperDao;
import com.sxt.pojo.Paper;
import com.sxt.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class PaperServiceImpl implements PaperService {@AutowiredPaperDao paperDao;@Overridepublic Integer addPaper(Paper paper) {return paperDao.addPaper(paper);}@Overridepublic List<Paper> getByEid(Integer eid) {return paperDao.getByEid(eid);}
}
ExamMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTO Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sxt.dao.ExamDao"><insert id="addExam" useGeneratedKeys="true" keyProperty="eid">insert into exam (eid,pname,cno,useid,classid,singlenumber,singlecore,examdate,examtime,testtime)values (default,#{pname},#{cno},#{useid},#{classid},#{singlenumber},#{singlecore},#{examdate},#{examtime},#{testtime})</insert><update id="updExam">update exam set pname=#{pname},cno=#{cno},useid=#{useid},classid=#{classid},singlenumber=#{singlenumber},singlecore=#{singlecore},examdate=#{examdate},examtime=#{examtime},testtime=#{testtime}where eid=#{eid}</update><delete id="delExam">delete from exam where eid=#{eid}</delete><select id="getAllExam" resultType="com.sxt.pojo.Exam">select * from exam</select><select id="getExam" resultType="com.sxt.pojo.Exam">select * from exam where eid=#{eid}</select>
</mapper>
PaperMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTO Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sxt.dao.PaperDao"><insert id="addPaper">insert into paper(pid,eid,sid,cno,scontent,sa,sb,sc,sd,skey)values (default,#{eid},#{sid},#{cno},#{scontent},#{sa},#{sb},#{sc},#{sd},#{skey})</insert><delete id="deleteByEid">delete from paper where eid=#{eid}</delete><select id="getByEid" resultType="com.sxt.pojo.Paper">select * from paper where eid=#{eid}</select>
</mapper>
老师退出
TeaUserController
package com.sxt.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;import javax.servlet.http.HttpServletRequest;@Controller
public class TeaUserController {//退出登录@GetMapping("/Tlogout")public String Tlogout(HttpServletRequest request){//session请查看LoginController文件//需要清除老师id的sessionrequest.getSession().removeAttribute("Teauserid");//需要清除老师班级id的sessionrequest.getSession().removeAttribute("TeaClassid");//需要清除老师真实姓名的sessionrequest.getSession().removeAttribute("Teatruename");return "redirect:/toTeacher";//跳转到老师登录的页面}
}
运行结果:
学生修改密码
LoginController
package com.sxt.controller;import com.sxt.pojo.TeaUser;
import com.sxt.pojo.Users;import com.sxt.service.TeaUserService;
import com.sxt.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;import javax.servlet.http.HttpServletRequest;@Controllerpublic class LoginController {//学生登陆@PostMapping("/stulogin")public String stulogin(String username, String userpwd, HttpServletRequest servletRequest){if (login==null){}else {System.out.println("登陆成功");//将用户密码传入前端servletRequest.getSession().setAttribute("liss",userpwd);//调用获取用户ID的方法Integer userid = usersService.getByNameId(username);//将获取到的用户ID传入前端servletRequest.getSession().setAttribute("lis",userid);return "student/StuMan";}}}
StuUserController
package com.sxt.controller;import com.sxt.pojo.PClass;
import com.sxt.pojo.Users;
import com.sxt.service.PClassService;
import com.sxt.service.UsersService;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;import javax.servlet.http.HttpServletRequest;
import java.util.List;@Controller
public class StuUserController {//学生修改登录密码/*根据前端请求方式和端口,来写注解<form id="loginForm" th:action="@{/xiugai}" method="post" >*/@PostMapping("/xiugai")public String updateUserPwd(String userpwd,Integer userid){Integer i = usersService.updUserPwd(userpwd, userid);if(i==0){return "";}else{//修改成功,返回登录页面return "redirect:/toStuLogin";}}
}
UsersDao
package com.sxt.dao;import com.sxt.pojo.Users;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface UsersDao {//修改密码Integer updUserPwd(String userpwd,Integer userid);//通过用户名查询用户idInteger getByNameId(String username);
}
UsersMapper.xml
<!--学生修改登录密码--><update id="updUserPwd">update users set userpwd=#{userpwd}where userid=#{userid}</update><!--通过用户名查询用户id--><select id="getByNameId" resultType="java.lang.Integer">select userid from users where username=#{username}</select>
UsersService
package com.sxt.service;import com.sxt.pojo.Users;import java.util.List;public interface UsersService {//学生修改登录密码的接口Integer updUserPwd(String userpwd,Integer userid);//通过用户名查询用户idInteger getByNameId(String username);
}
UsersServiceImpl
package com.sxt.service.impl;import com.sxt.dao.UsersDao;
import com.sxt.pojo.Users;
import com.sxt.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;
@Service
public class UsersServiceImpl implements UsersService {//学生修改登录密码的实现方法,封装成一个方法,再调用@Overridepublic Integer updUserPwd(String userpwd, Integer userid) {return usersDao.updUserPwd(userpwd,userid);}//通过用户名查询用户id@Overridepublic Integer getByNameId(String username) {return usersDao.getByNameId(username);}
}
运行结果:
学生首页完善(添加姓名和退出登录)
LoginController
package com.sxt.controller;import com.sxt.pojo.TeaUser;
import com.sxt.pojo.Users;import com.sxt.service.TeaUserService;
import com.sxt.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;import javax.servlet.http.HttpServletRequest;@Controllerpublic class LoginController {//学生登陆@PostMapping("/stulogin")public String stulogin(String username, String userpwd, HttpServletRequest servletRequest){if (login==null){}else {//获取用户的真实姓名String trueName = usersService.getTrueName(username);//将获取到的用户的真实姓名传入前端,即在前端显示学生的真实姓名servletRequest.getSession().setAttribute("truename",trueName);return "student/StuMan";}}
}
StuUserController
package com.sxt.controller;import com.sxt.pojo.PClass;
import com.sxt.pojo.Users;
import com.sxt.service.PClassService;
import com.sxt.service.UsersService;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;import javax.servlet.http.HttpServletRequest;
import java.util.List;@Controller
public class StuUserController {//退出登录@GetMapping("/logout")public String slogout(HttpServletRequest request){//清除用户ID的sessionrequest.getSession().removeAttribute("lis");//清除用户真实姓名的sessionrequest.getSession().removeAttribute("truename");return "redirect:/toStuLogin";}
}
UsersDao
package com.sxt.dao;import com.sxt.pojo.Users;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface UsersDao {//通过用户名查询学生的真实姓名String getTrueName(String username);
}
UsersMapper.xml
<!--通过用户名查询学生的真实姓名--><select id="getTrueName" resultType="java.lang.String">select truename from users where username=#{username}</select>
UsersService
package com.sxt.service;import com.sxt.pojo.Users;import java.util.List;public interface UsersService {//通过用户名查询学生的真实姓名String getTrueName(String username);
}
UsersServiceImpl
package com.sxt.service.impl;import com.sxt.dao.UsersDao;
import com.sxt.pojo.Users;
import com.sxt.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;
@Service
public class UsersServiceImpl implements UsersService {//通过用户名查询学生真实姓名@Overridepublic String getTrueName(String username) {return usersDao.getTrueName(username);}
}
运行结果:
考试中心的考试信息列表
ExamController
package com.sxt.controller;import com.sxt.pojo.Course;
import com.sxt.pojo.Exam;
import com.sxt.service.CourseService;
import com.sxt.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;import javax.servlet.http.HttpServletRequest;
import java.util.List;@Controller
public class ExamController {@AutowiredExamService examService;@AutowiredCourseService courseService;@GetMapping("/examList")public String examList(HttpServletRequest request, Model model){//获取班级IDInteger classid = (Integer) request.getSession().getAttribute("classid");List<Exam> examClassid = examService.getExamClassid(classid);//获取课程IDfor (Exam exam : examClassid){Course allById = courseService.getAllById(exam.getCno());exam.setCourse(allById);}//把课程ID传入前端model.addAttribute("examslenth",examClassid.size());model.addAttribute("exams",examClassid);return "student/examList";}
}
LoginController
package com.sxt.controller;import com.sxt.pojo.TeaUser;
import com.sxt.pojo.Users;import com.sxt.service.TeaUserService;
import com.sxt.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;import javax.servlet.http.HttpServletRequest;@Controllerpublic class LoginController {//学生登陆@PostMapping("/stulogin")public String stulogin(String username, String userpwd, HttpServletRequest servletRequest){if (login==null){}else {//通过用户名查询班级IDInteger byClass = usersService.getByClass(username);//将获取到的班级ID传入前端servletRequest.getSession().setAttribute("classid",byClass);return "student/StuMan";}}
}
ExamDao
package com.sxt.dao;import com.sxt.pojo.Exam;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface ExamDao {//通过班级id查询考试信息List<Exam> getExamClassid(Integer classid);
}
UsersDao
package com.sxt.dao;import com.sxt.pojo.Users;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface UsersDao {//通过用户名查询班级IDInteger getByClass(String username);
}
ExamMapper.xml
<!--通过班级id查询考试信息--><select id="getExamClassid" resultType="com.sxt.pojo.Exam">select * from exam where classid=#{classid}</select>
UsersMapper.xml
<!--通过用户名查询班级ID--><select id="getByClass" resultType="java.lang.Integer">select classid from users where username=#{username}</select>
ExamService
package com.sxt.service;import com.sxt.pojo.Exam;import java.util.List;public interface ExamService {//通过班级id查询考试信息List<Exam> getExamClassid(Integer classid);
}
UsersService
package com.sxt.service;import com.sxt.pojo.Users;import java.util.List;public interface UsersService {//通过用户名查询班级IDInteger getByClass(String username);
}
ExamServiceImpl
package com.sxt.service.impl;import com.sxt.dao.ExamDao;
import com.sxt.dao.PaperDao;
import com.sxt.pojo.Exam;
import com.sxt.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class ExamServiceImpl implements ExamService {//通过班级id查询考试信息@Overridepublic List<Exam> getExamClassid(Integer classid) {return examDao.getExamClassid(classid);}
}
UsersSerivceImpl
package com.sxt.service.impl;import com.sxt.dao.UsersDao;
import com.sxt.pojo.Users;
import com.sxt.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;
@Service
public class UsersServiceImpl implements UsersService {//通过用户名查询班级ID@Overridepublic Integer getByClass(String username) {return usersDao.getByClass(username);}
}
运行结果:
注意创建数据测试时,创建考试的老师所在班级ID和学生所在班级ID要一致
答卷
ExamController
package com.sxt.controller;import com.sxt.pojo.Course;
import com.sxt.pojo.Exam;
import com.sxt.pojo.Paper;
import com.sxt.service.CourseService;
import com.sxt.service.ExamService;
import com.sxt.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;
import java.util.List;@Controller
public class ExamController {@AutowiredPaperService paperService;//查询数据@ResponseBody@PostMapping("/findExamByEid")public Exam findExamById(@RequestBody Exam exam){Exam exam1 = examService.getExam(exam.getEid());if(exam1!=null){//如果不为空,返回考试信息return exam1;}else{return null;}}//查询试卷@GetMapping("/paper")public String findPaper(Integer eid,HttpServletRequest request,Model model){//通过考试信息ID获取试卷问题列表List<Paper> byEid = paperService.getByEid(eid);int size=byEid.size();//获取考试信息Exam exam = examService.getExam(eid);//传入前端request.getSession().setAttribute("single",byEid);model.addAttribute("cont",size);model.addAttribute("exam",exam);return "student/papers";}
}
运行结果:
显示考试成绩
Studentexam
package com.sxt.pojo;public class Studentexam {private Integer seid;private Integer userid;private Integer classid;private Integer eid;private String pname;private Integer score;private Integer zscore;private String tjtime;//getter,setter方法省略
}
ExamController
package com.sxt.controller;import com.sun.org.apache.xpath.internal.operations.Mod;
import com.sxt.pojo.Course;
import com.sxt.pojo.Exam;
import com.sxt.pojo.Paper;
import com.sxt.pojo.Studentexam;
import com.sxt.service.CourseService;
import com.sxt.service.ExamService;
import com.sxt.service.PaperService;
import com.sxt.service.StudentExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;@Controller
public class ExamController {@AutowiredStudentExamService studentExamService;//显示考试成绩@PostMapping("/PaperScore")public String paperScore(HttpServletRequest request,Model model){//存放答案String stuArr[]=null;//初始成绩Integer score=0;//计算成绩Integer eid = Integer.valueOf(request.getParameter("eid"));Exam exam = examService.getExam(eid);Integer singlecore = exam.getSinglecore();//获取试卷问题列表List<Paper> single = (List<Paper>) request.getSession().getAttribute("single");//获取试卷ID的值for(int i=0;i<single.size();i++){Paper paper = single.get(i);stuArr = request.getParameterValues(String.valueOf(paper.getSid()));if(stuArr!=null){//如果数据不为空就进行答案封装String stuKeys="";//封装答案for(int j=0;j<stuArr.length;j++){stuKeys+=stuArr[i];}if(stuKeys.equalsIgnoreCase(paper.getSkey())){score+=singlecore;}else{}}else{//否则返回考试信息列表页面return "redirect:/examList";}}//计算总成绩int zscore=single.size()+singlecore;//学生成绩model.addAttribute("score",score);String pname = request.getParameter("pname");String tjtime = request.getParameter("tjtime");Timestamp timestamp = new Timestamp(System.currentTimeMillis());Timestamp.valueOf(tjtime);//获取classid的值Integer classid = (Integer) request.getSession().getAttribute("classid");//获取userid的值Integer userid = (Integer) request.getSession().getAttribute("lis");Studentexam studentexam = new Studentexam();//用set方法存储值studentexam.setEid(eid);studentexam.setPname(pname);studentexam.setScore(score);studentexam.setClassid(classid);studentexam.setUserid(userid);studentexam.setTjtime(tjtime);studentexam.setZscore(zscore);studentExamService.addStudentExam(studentexam);return "student/paperScore";}//查询学生成绩@GetMapping("/findAllStuPaper")public String findStuPaperList(HttpServletRequest request, Model model){//获取用户IDInteger userid = (Integer) request.getSession().getAttribute("lis");//根据用户ID查询学生成绩List<Studentexam> studentexams = studentExamService.getstuExamList(userid);//传入前端model.addAttribute("stuexamlist",studentexams);return "student/stuPaperList";}
}
StudentExamDao
package com.sxt.dao;import com.sxt.pojo.Studentexam;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface StudentExamDao {//添加学生成绩Integer addStudentExam(Studentexam studentexam);//查询学生成绩List<Studentexam> getStuPaperList(Integer userid);
}
StudentExamMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sxt.dao.StudentExamDao"><!--添加学生成绩--><insert id="addStudentExam">insert into studentrxam(seid,userid,classid,eid,pname,zscore,score,tjtime)values(default ,#{userid},#{classid},#{eid},#{pname},#{zscore},#{score},#{tjtime})</insert><!--查询学生成绩--><select id="getStuPaperList" resultType="com.sxt.pojo.Studentexam">select * from studentrxam where userid=#{userid}</select>
</mapper>
StudentExamSerivce
package com.sxt.service;import com.sxt.pojo.Studentexam;import java.util.List;public interface StudentExamService {//添加学生成绩Integer addStudentExam(Studentexam studentexam);//查询学生成绩List<Studentexam> getstuExamList(Integer userid);
}
StudentExamServiceImpl
package com.sxt.service.impl;import com.sxt.dao.StudentExamDao;
import com.sxt.pojo.Studentexam;
import com.sxt.service.StudentExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class StudentExamServiceImpl implements StudentExamService {@AutowiredStudentExamDao studentExamDao;//添加学生成绩@Overridepublic Integer addStudentExam(Studentexam studentexam) {return studentExamDao.addStudentExam(studentexam);}//查询学生成绩@Overridepublic List<Studentexam> getstuExamList(Integer userid) {return studentExamDao.getStuPaperList(userid);}
}
运行结果:
学生考试功能的完善
ExamController
package com.sxt.controller;import com.sun.org.apache.xpath.internal.operations.Mod;
import com.sxt.pojo.Course;
import com.sxt.pojo.Exam;
import com.sxt.pojo.Paper;
import com.sxt.pojo.Studentexam;
import com.sxt.service.CourseService;
import com.sxt.service.ExamService;
import com.sxt.service.PaperService;
import com.sxt.service.StudentExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;@Controller
public class ExamController {//是否做过该试卷,不可重复考试@ResponseBody@PostMapping("/findOneStuExam")public List<Studentexam> findIsPaper(HttpServletRequest request,@RequestBody Exam exam){//获取学生IDInteger userid = (Integer) request.getSession().getAttribute("lis");//获取考试信息IDInteger eid = exam.getEid();//是否做过该试卷List<Studentexam> isPaper = studentExamService.getIsPaper(userid, eid);return isPaper;}//解决点击在线考试报错@GetMapping("/StuMan")public String getStuMan(){return "student/StuMan";//跳转到在线考试系统页面即可}
}
StudentExamDao
package com.sxt.dao;import com.sxt.pojo.Studentexam;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface StudentExamDao {//查询是否做过该试卷List<Studentexam> getIsPaper(Integer userid,Integer eid);
}
StudentExamMapper.xml
<select id="getIsPaper" resultType="com.sxt.pojo.Studentexam">select * from studentrxam where userid=#{userid} and eid=#{eid}</select>
StudentExamService
package com.sxt.service;import com.sxt.pojo.Studentexam;import java.util.List;public interface StudentExamService {//查询是否做过该试卷List<Studentexam> getIsPaper(Integer userid,Integer eid);
}
StudentExamServiceImpl
package com.sxt.service.impl;import com.sxt.dao.StudentExamDao;
import com.sxt.pojo.Studentexam;
import com.sxt.service.StudentExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class StudentExamServiceImpl implements StudentExamService {//查询是否做过该试卷@Overridepublic List<Studentexam> getIsPaper(Integer userid, Integer eid) {return studentExamDao.getIsPaper(userid,eid);}
}
运行结果:
教师查询学生成绩信息
ExamController
package com.sxt.controller;import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.sxt.pojo.*;
import com.sxt.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;@Controller
public class ExamController {//老师查询学生成绩@GetMapping("/findAllScore")public String findAllScore(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,HttpServletRequest request, Model model){//实现分页功能PageHelper.startPage(pageNum,5);//每5条分一页//获取老师班级IDInteger teaClassid = (Integer) request.getSession().getAttribute("TeaClassid");//通过班级ID获取班级信息PClass classes = pClassService.getClassid(teaClassid);//信息传入前端model.addAttribute("cs",classes);//老师查询学生成绩List<Studentexam> allStuScore = studentExamService.getAllStuScore(teaClassid);//通过成绩获取学生信息for(Studentexam studentexam : allStuScore){Users byUserid = usersService.getByUserid(studentexam.getUserid());studentexam.setUsers(byUserid);}//传入前端 PageInfo<Studentexam> studentexamPageInfo = new PageInfo<>(allStuScore);model.addAttribute("pageInfo",studentexamPageInfo);model.addAttribute("score",allStuScore);return "teacher/studentScore";}}
StudentExamDao
package com.sxt.dao;import com.sxt.pojo.Studentexam;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface StudentExamDao {//老师查询试卷信息List<Studentexam> getAllStuScore(Integer classid);
}
StudentExamMapper.xml
<!--老师查询试卷信息--><select id="getAllStuScore" resultType="com.sxt.pojo.Studentexam">select * from studentexam where classid=#{classid}</select>
StudentExamService
package com.sxt.service;import com.sxt.pojo.Studentexam;import java.util.List;public interface StudentExamService {//老师查询试卷信息List<Studentexam> getAllStuScore(Integer classid);
}
StudentExamServiceImpl
package com.sxt.service.impl;import com.sxt.dao.StudentExamDao;
import com.sxt.pojo.Studentexam;
import com.sxt.service.StudentExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class StudentExamServiceImpl implements StudentExamService {//老师查询试卷信息@Overridepublic List<Studentexam> getAllStuScore(Integer classid) {return studentExamDao.getAllStuScore(classid);}
}
运行结果:
配置登录拦截器
LoginInterceptor
package com.sxt.config;import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*** 配置登录拦截器*/
public class LoginInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//学生IDInteger userid = (Integer) request.getSession().getAttribute("lis");//老师IDInteger teauserid = (Integer) request.getSession().getAttribute("Teauserid");//获取URIString uri=request.getRequestURI();if(userid!=null){if(!(uri.equals("/StudentList"))&&!(uri.equals("/DeleteStu"))&&!(uri.equals("/deleteAll"))&&!(uri.equals("/finddanxuan"))&&!(uri.equals("/addSingle"))&&!(uri.equals("/selectexam"))&&!(uri.equals("/addexam"))&&!(uri.equals("/paperDetails"))&&!(uri.equals("/deleteExam"))&&!(uri.equals("/findAllScore"))){return true;}}if(teauserid!=null){if(!(uri.equals("/examList"))&&!(uri.equals("/paper"))&&!(uri.equals("/findAllStuPaper"))&&!(uri.equals("/StuMan"))){return true;}}response.sendRedirect(request.getContextPath()+"/toStuLogin");//跳转回登录页面//返回登录页时清空学生和老师的登录信息request.getSession().removeAttribute("lis");request.getSession().removeAttribute("Teauserid");return false;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}
}
LoginConfig
package com.sxt.config;import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/*** 声明登录拦截器的类*/
@Configuration
public class LoginConfig implements WebMvcConfigurer {@Overridepublic void addInterceptors(InterceptorRegistry registry) {//注册登录拦截器InterceptorRegistration interceptorRegistration = registry.addInterceptor(new LoginInterceptor());//拦截所有路径interceptorRegistration.addPathPatterns("/**");//不用拦截的路径:登录页面和静态资源interceptorRegistration.excludePathPatterns("/css/**","/fonts/**","/img/**","/js/**","/","/student/login.html","/student/login.html","student/register.html","/toTeacher","/toStuLogin","/stulogin","/tealogin","/register","/CanRegister");}
}
运行结果: