基于javaweb+mysql的springboot+mybatis在线考试系统(教师、学生)(java+springboot+jsp+mysql)
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
教师:查看考试结果、编辑考试题目
学生:考试答题
老师
学生
技术框架
JSP JavaScript Bootstrap SpringBoot SpringMVC MyBatis
基于javaweb+mysql的SpringBoot+MyBatis在线考试系统(教师、学生)(java+springboot+jsp+mysql)
} else if (name.startsWith(Const.PAGE_NAME_PREFIX_FILLBLANK)) {
stuAnswer = request.getParameter(name);
setUserAnswer(name, Const.PAGE_NAME_PREFIX_FILLBLANK, fillblanks, stuAnswer);
} else if (name.startsWith(Const.PAGE_NAME_PREFIX_JUDGE)) {
stuAnswer = request.getParameter(name);
setUserAnswer(name, Const.PAGE_NAME_PREFIX_JUDGE, judgements, stuAnswer);
} else if (name.startsWith(Const.PAGE_NAME_PREFIX_WRITE)) {
stuAnswer = request.getParameter(name);
setUserAnswer(name, Const.PAGE_NAME_PREFIX_WRITE, writes, stuAnswer);
}
}
// Paper中設置含答案的試題對象
paper.setChoices(choices);
paper.setFillblanks(fillblanks);
paper.setJudgements(judgements);
paper.setWrites(writes);
// 獲取結果
// - 创建PaperAnswer对象和PaperSubjectiveAnswer列表
PaperAnswer paperAnswer = new PaperAnswer();
List<PaperSubjectiveAnswer> list = new ArrayList<>();
// - 获取答题学生ID
User stu = (User) session.getAttribute(Const.SESSION_STU);
paperAnswer.setStuId(stu.getId());
// - 获取当前时间
paperAnswer.setTime(new Date());
// - 获取当前判分结果,以及简答题列表
String html = paper.getCurrentResult(paperAnswer, list);
m.addAttribute("paperResult", html);
// - 保存进数据库
service.save(paperAnswer, list);
return Const.PAGE_STUDENT_RESULT;
}
private void setUserAnswer(String paraName, String typePrefix, List<QuestionPlus> qList, String userAnswer) {
// 获取试题原始对象
int len = typePrefix.length();
String i = paraName.substring(len);
int paperId = Integer.parseInt(i);// 试题编号
int index = paperId - 1;
QuestionPlus qq = qList.get(index);
// 设置题目编号
qq.setPaperId(paperId);
// 设置用户答案
// 使用和Admin端一样的Service
private QuestionService service;
/**
* 题目保存
*
* @param q:题目
* @return action:查询全部题目
*/
@RequestMapping("save")
public String saveQuestion(Question q) {
service.save(q);
return "redirect:all";
}
// 查询所有,将List对象传给model
@RequestMapping("all")
public String allQuestion(Model model) {
List<Question> findAll = service.getAllQuestions();
model.addAttribute("list", findAll);
return Const.PAGE_QUESTION_LIST;
}
// 删除
@RequestMapping("delete")
public String deleteQuestion(String id) {
System.out.println("即将删除" + service.getQuestionById(Integer.valueOf(id)));
int ret = service.delete(Integer.valueOf(id));
System.out.println("delete:" + ret);
return "redirect:all";
}
// 编辑
@RequestMapping("edit")
public String editQuestion(String id, Model model) {
Question q = service.getQuestionById(Integer.valueOf(id));
model.addAttribute("question", q);
return Const.PAGE_QUESTION_EDIT;
}
// 添加(进编辑页面)
// 编辑和添加都会进入同一个界面,区别是编辑会先根据id查找Bean对象传入该界面而添加不会
@RequestMapping("add")
public String addQuestion() {
return Const.PAGE_QUESTION_EDIT;
}
}
private QuestionService service;
/**
* 题目保存
*
* @param q:题目
* @return action:查询全部题目
*/
@RequestMapping("save")
public String saveQuestion(Question q) {
service.save(q);
return "redirect:all";
}
// 查询所有,将List对象传给model
@RequestMapping("all")
public String allQuestion(Model model) {
List<Question> findAll = service.getAllQuestions();
model.addAttribute("list", findAll);
return Const.PAGE_QUESTION_LIST;
}
// 删除
@RequestMapping("delete")
public String deleteQuestion(String id) {
System.out.println("即将删除" + service.getQuestionById(Integer.valueOf(id)));
int ret = service.delete(Integer.valueOf(id));
System.out.println("delete:" + ret);
return "redirect:all";
}
// 编辑
@RequestMapping("edit")
public String editQuestion(String id, Model model) {
Question q = service.getQuestionById(Integer.valueOf(id));
model.addAttribute("question", q);
return Const.PAGE_QUESTION_EDIT;
}
// 添加(进编辑页面)
// 编辑和添加都会进入同一个界面,区别是编辑会先根据id查找Bean对象传入该界面而添加不会
@RequestMapping("add")
public String addQuestion() {
return Const.PAGE_QUESTION_EDIT;
}
}
private QuestionService service;
// 生成试卷
@RequestMapping("showpaper")
public String method(HttpServletRequest request) {
System.out.println("===show paper");
HttpSession session = request.getSession();
List<Question> allQuestions = service.getAllQuestions();
QuestionPaper paper = new QuestionPaper();
Random r = new Random();
for (int i = 0; i < 5; i++) {
// 随机取题
int nextInt = r.nextInt(allQuestions.size());
Question q = allQuestions.get(nextInt);
QuestionPlus qPlus = new QuestionPlus();
qPlus.setQ(q);
// 区分题型
int type= (q.getType() == null)?0:q.getType();
// 加入试卷
paper.addInQsList(qPlus, type);
allQuestions.remove(nextInt);
}
session.setAttribute("paper", paper);
return Const.PAGE_PAPER;
}
}
@Controller
@RequestMapping("user")
public class LoginController {
@Autowired
LoginService service;
@RequestMapping("login")
public String login(HttpServletRequest request, String username, String pwd, String role) {
if ((username != null) && (pwd != null)) {
User u = new User();
u.setName(username);
public String allQuestion(Model model) {
List<Question> findAll = service.getAllQuestions();
model.addAttribute("list", findAll);
return Const.PAGE_QUESTION_LIST;
}
// 删除
@RequestMapping("delete")
public String deleteQuestion(String id) {
System.out.println("即将删除" + service.getQuestionById(Integer.valueOf(id)));
int ret = service.delete(Integer.valueOf(id));
System.out.println("delete:" + ret);
return "redirect:all";
}
// 编辑
@RequestMapping("edit")
public String editQuestion(String id, Model model) {
Question q = service.getQuestionById(Integer.valueOf(id));
model.addAttribute("question", q);
return Const.PAGE_QUESTION_EDIT;
}
// 添加(进编辑页面)
// 编辑和添加都会进入同一个界面,区别是编辑会先根据id查找Bean对象传入该界面而添加不会
@RequestMapping("add")
public String addQuestion() {
return Const.PAGE_QUESTION_EDIT;
}
}
@Controller
@RequestMapping("question")
public class QuestionStudentController {
@Controller
@RequestMapping("question")
public class ScoreController {
@Autowired
private ScoreService service;
// 判分
@RequestMapping("score")
public String score(HttpServletRequest request, Model m) {
HttpSession session = request.getSession();
QuestionPaper paper = (QuestionPaper) session.getAttribute(Const.SESSION_PAPER);
List<QuestionPlus> choices = paper.getChoices();
List<QuestionPlus> fillblanks = paper.getFillblanks();
List<QuestionPlus> judgements = paper.getJudgements();
List<QuestionPlus> writes = paper.getWrites();
// --- 获取用户答案 ---
Enumeration<String> names = request.getParameterNames();
String stuAnswer = "";
while (names.hasMoreElements()) {
stuAnswer = "";
String name = names.nextElement();
if (name.startsWith(Const.PAGE_NAME_PREFIX_CHOICE)) {
String[] stuChoiceAnswer = request.getParameterValues(name);
for (String s : stuChoiceAnswer) {
stuAnswer += s;
}
setUserAnswer(name, Const.PAGE_NAME_PREFIX_CHOICE, choices, stuAnswer);
} else if (name.startsWith(Const.PAGE_NAME_PREFIX_FILLBLANK)) {
stuAnswer = request.getParameter(name);
setUserAnswer(name, Const.PAGE_NAME_PREFIX_FILLBLANK, fillblanks, stuAnswer);
} else if (name.startsWith(Const.PAGE_NAME_PREFIX_JUDGE)) {
stuAnswer = request.getParameter(name);
setUserAnswer(name, Const.PAGE_NAME_PREFIX_JUDGE, judgements, stuAnswer);
} else if (name.startsWith(Const.PAGE_NAME_PREFIX_WRITE)) {
stuAnswer = request.getParameter(name);
private QuestionService service;
// 生成试卷
@RequestMapping("showpaper")
public String method(HttpServletRequest request) {
System.out.println("===show paper");
HttpSession session = request.getSession();
List<Question> allQuestions = service.getAllQuestions();
QuestionPaper paper = new QuestionPaper();
Random r = new Random();
for (int i = 0; i < 5; i++) {
// 随机取题
int nextInt = r.nextInt(allQuestions.size());
Question q = allQuestions.get(nextInt);
QuestionPlus qPlus = new QuestionPlus();
qPlus.setQ(q);
// 区分题型
int type= (q.getType() == null)?0:q.getType();
// 加入试卷
paper.addInQsList(qPlus, type);
allQuestions.remove(nextInt);
}
session.setAttribute("paper", paper);
return Const.PAGE_PAPER;
}
}
@Controller
@RequestMapping("user")
public class LoginController {
@Autowired
LoginService service;
// 删除
@RequestMapping("delete")
public String deleteQuestion(String id) {
System.out.println("即将删除" + service.getQuestionById(Integer.valueOf(id)));
int ret = service.delete(Integer.valueOf(id));
System.out.println("delete:" + ret);
return "redirect:all";
}
// 编辑
@RequestMapping("edit")
public String editQuestion(String id, Model model) {
Question q = service.getQuestionById(Integer.valueOf(id));
model.addAttribute("question", q);
return Const.PAGE_QUESTION_EDIT;
}
// 添加(进编辑页面)
// 编辑和添加都会进入同一个界面,区别是编辑会先根据id查找Bean对象传入该界面而添加不会
@RequestMapping("add")
public String addQuestion() {
return Const.PAGE_QUESTION_EDIT;
}
}
@Controller
@RequestMapping("question")
public class QuestionStudentController {
@Autowired
private QuestionService service;
// 生成试卷
@Controller
@RequestMapping("question")
public class QuestionStudentController {
@Autowired
private QuestionService service;
// 生成试卷
@RequestMapping("showpaper")
public String method(HttpServletRequest request) {
System.out.println("===show paper");
HttpSession session = request.getSession();
List<Question> allQuestions = service.getAllQuestions();
QuestionPaper paper = new QuestionPaper();
Random r = new Random();
for (int i = 0; i < 5; i++) {
// 随机取题
int nextInt = r.nextInt(allQuestions.size());
Question q = allQuestions.get(nextInt);
QuestionPlus qPlus = new QuestionPlus();
qPlus.setQ(q);
// 区分题型
int type= (q.getType() == null)?0:q.getType();
// 加入试卷
paper.addInQsList(qPlus, type);
allQuestions.remove(nextInt);
}
session.setAttribute("paper", paper);
return Const.PAGE_PAPER;
}
}
String i = paraName.substring(len);
int paperId = Integer.parseInt(i);// 试题编号
int index = paperId - 1;
QuestionPlus qq = qList.get(index);
// 设置题目编号
qq.setPaperId(paperId);
// 设置用户答案
qq.setStuAnswer(userAnswer);
qList.set(index, qq);
}
@RequestMapping("mark")
public String mark(Model m) {
List<PaperAnswer> paperAnswers = service.selectAllPaperAnswer();
m.addAttribute("list", paperAnswers);
return Const.PAGE_ADMIN_MARK_LIST;
}
@RequestMapping("markDetail")
public String markDetail(String id, Model m) {
Integer nId = Integer.valueOf(id);
PaperAnswer answerDetail = service.getAnswerDetail(nId);
m.addAttribute("md", answerDetail);
return Const.PAGE_ADMIN_MARK_DETAIL;
}
}
@Controller
@RequestMapping("question")
/**
* 管理员试题操作
*
*
*/
public class QuestionAdminController {
@Autowired
// 使用和Admin端一样的Service
private QuestionService service;
paper.setChoices(choices);
paper.setFillblanks(fillblanks);
paper.setJudgements(judgements);
paper.setWrites(writes);
// 獲取結果
// - 创建PaperAnswer对象和PaperSubjectiveAnswer列表
PaperAnswer paperAnswer = new PaperAnswer();
List<PaperSubjectiveAnswer> list = new ArrayList<>();
// - 获取答题学生ID
User stu = (User) session.getAttribute(Const.SESSION_STU);
paperAnswer.setStuId(stu.getId());
// - 获取当前时间
paperAnswer.setTime(new Date());
// - 获取当前判分结果,以及简答题列表
String html = paper.getCurrentResult(paperAnswer, list);
m.addAttribute("paperResult", html);
// - 保存进数据库
service.save(paperAnswer, list);
return Const.PAGE_STUDENT_RESULT;
}
private void setUserAnswer(String paraName, String typePrefix, List<QuestionPlus> qList, String userAnswer) {
// 获取试题原始对象
int len = typePrefix.length();
String i = paraName.substring(len);
int paperId = Integer.parseInt(i);// 试题编号
int index = paperId - 1;
QuestionPlus qq = qList.get(index);
// 设置题目编号
qq.setPaperId(paperId);
// 设置用户答案
qq.setStuAnswer(userAnswer);
qList.set(index, qq);
}
@RequestMapping("mark")
public String mark(Model m) {
List<PaperAnswer> paperAnswers = service.selectAllPaperAnswer();
m.addAttribute("list", paperAnswers);
return Const.PAGE_ADMIN_MARK_LIST;
}
@RequestMapping("markDetail")
public String markDetail(String id, Model m) {
Integer nId = Integer.valueOf(id);
PaperAnswer answerDetail = service.getAnswerDetail(nId);
m.addAttribute("md", answerDetail);
return Const.PAGE_ADMIN_MARK_DETAIL;
int type= (q.getType() == null)?0:q.getType();
// 加入试卷
paper.addInQsList(qPlus, type);
allQuestions.remove(nextInt);
}
session.setAttribute("paper", paper);
return Const.PAGE_PAPER;
}
}
@Controller
@RequestMapping("user")
public class LoginController {
@Autowired
LoginService service;
@RequestMapping("login")
public String login(HttpServletRequest request, String username, String pwd, String role) {
if ((username != null) && (pwd != null)) {
User u = new User();
u.setName(username);
u.setPwd(pwd);
u.setRole(role);
u = service.getUser(u);
if (u != null) {
HttpSession session = request.getSession();
session.setAttribute(Const.SESSION_STU, u);
// 分为学生和管理员
return userMenu(u.getRole());
}
}
request.setAttribute("loginMsg", "登录失败!");
return "forward:/login.jsp";
paper.addInQsList(qPlus, type);
allQuestions.remove(nextInt);
}
session.setAttribute("paper", paper);
return Const.PAGE_PAPER;
}
}
@Controller
@RequestMapping("user")
public class LoginController {
@Autowired
LoginService service;
@RequestMapping("login")
public String login(HttpServletRequest request, String username, String pwd, String role) {
if ((username != null) && (pwd != null)) {
User u = new User();
u.setName(username);
u.setPwd(pwd);
u.setRole(role);
u = service.getUser(u);
if (u != null) {
HttpSession session = request.getSession();
session.setAttribute(Const.SESSION_STU, u);
// 分为学生和管理员
return userMenu(u.getRole());
}
}
request.setAttribute("loginMsg", "登录失败!");
return "forward:/login.jsp";
public String saveQuestion(Question q) {
service.save(q);
return "redirect:all";
}
// 查询所有,将List对象传给model
@RequestMapping("all")
public String allQuestion(Model model) {
List<Question> findAll = service.getAllQuestions();
model.addAttribute("list", findAll);
return Const.PAGE_QUESTION_LIST;
}
// 删除
@RequestMapping("delete")
public String deleteQuestion(String id) {
System.out.println("即将删除" + service.getQuestionById(Integer.valueOf(id)));
int ret = service.delete(Integer.valueOf(id));
System.out.println("delete:" + ret);
return "redirect:all";
}
// 编辑
@RequestMapping("edit")
public String editQuestion(String id, Model model) {
Question q = service.getQuestionById(Integer.valueOf(id));
model.addAttribute("question", q);
return Const.PAGE_QUESTION_EDIT;
}
// 添加(进编辑页面)
// 编辑和添加都会进入同一个界面,区别是编辑会先根据id查找Bean对象传入该界面而添加不会
@RequestMapping("add")
public String addQuestion() {
return Const.PAGE_QUESTION_EDIT;
}
}
}
// 添加(进编辑页面)
// 编辑和添加都会进入同一个界面,区别是编辑会先根据id查找Bean对象传入该界面而添加不会
@RequestMapping("add")
public String addQuestion() {
return Const.PAGE_QUESTION_EDIT;
}
}
@Controller
@RequestMapping("question")
public class QuestionStudentController {
@Autowired
private QuestionService service;
// 生成试卷
@RequestMapping("showpaper")
public String method(HttpServletRequest request) {
System.out.println("===show paper");
HttpSession session = request.getSession();
List<Question> allQuestions = service.getAllQuestions();
@RequestMapping("question")
/**
* 管理员试题操作
*
*
*/
public class QuestionAdminController {
@Autowired
// 使用和Admin端一样的Service
private QuestionService service;
/**
* 题目保存
*
* @param q:题目
* @return action:查询全部题目
*/
@RequestMapping("save")
public String saveQuestion(Question q) {
service.save(q);
return "redirect:all";
}
// 查询所有,将List对象传给model
@RequestMapping("all")
public String allQuestion(Model model) {
List<Question> findAll = service.getAllQuestions();
model.addAttribute("list", findAll);
return Const.PAGE_QUESTION_LIST;
}
// 删除
@RequestMapping("delete")
public String deleteQuestion(String id) {
System.out.println("即将删除" + service.getQuestionById(Integer.valueOf(id)));
int ret = service.delete(Integer.valueOf(id));
System.out.println("delete:" + ret);
return "redirect:all";
}
// 编辑
@RequestMapping("edit")
public String editQuestion(String id, Model model) {
// 加入试卷
paper.addInQsList(qPlus, type);
allQuestions.remove(nextInt);
}
session.setAttribute("paper", paper);
return Const.PAGE_PAPER;
}
}
@Controller
@RequestMapping("user")
public class LoginController {
@Autowired
LoginService service;
@RequestMapping("login")
public String login(HttpServletRequest request, String username, String pwd, String role) {
if ((username != null) && (pwd != null)) {
User u = new User();
u.setName(username);
u.setPwd(pwd);
u.setRole(role);
u = service.getUser(u);
if (u != null) {
HttpSession session = request.getSession();
session.setAttribute(Const.SESSION_STU, u);
// 分为学生和管理员
return userMenu(u.getRole());
}
}
request.setAttribute("loginMsg", "登录失败!");
return "forward:/login.jsp";
}
@RequestMapping("menu")
public String userMenu(String role) {