开发工具:idea
数据库mysql5.7+(mysql5.7最佳)
数据库链接工具:navcat,小海豚等
开发技术:java springboot html
【503】基于springboot在线考试管理系统源码
后端技术:springboot2.2 springmvc mybatis
前端技术:bootstrap thymeleaf html
开发环境:maven jdk1.8 mysql idea
管理员后台:http://localhost:8080/backLogin teacher表 账号密码:zhaosi 123456
组题数量不能超过实际未分配的试题数量
package com.zhao.quiz.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhao.quiz.domain.*;
import com.zhao.quiz.mapper.QuestionMapper;
import com.zhao.quiz.service.ClasseService;
import com.zhao.quiz.service.PaperService;
import com.zhao.quiz.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.*;
@Controller
@RequestMapping("/paper")
public class PaperController {
@Autowired
private PaperService paperService;
@Autowired
private ClasseService classeService;
@Autowired
private QuestionService questionService;
//查看所有试卷
@RequestMapping("/getAllPaper")
public String getAllPaper(Model model){
List<Paper> papers = paperService.getAll();
//查找classe表中已存在的试卷,将用于表单试卷是否可以删除
model.addAttribute("papers",papers);
return "paper/paperList";
}
//试卷添加或者修改操作,先去添加页面
@RequestMapping("/toAddPaper")
public String toAddPaper(){
return "paper/paperAdd";
}
//添加具体操作
@RequestMapping("/addPaper")
public String addPaper(Paper paper){
paperService.addPaper(paper);
return "redirect:/paper/getAllPaper";
}
//试卷去修改页面
@RequestMapping("/toEditPaper/{id}")
public String toEditPaper(@PathVariable("id") Integer id,Model model){
Paper paper=paperService.getPaperById(id);
model.addAttribute("paper",paper);
return "paper/paperEdit";
}
//试卷修改
@RequestMapping("/EditPaper")
public String toEditPaper(Paper paper){
paperService.editPaper(paper);
return "redirect:/paper/getAllPaper";
}
//试卷删除
@RequestMapping("/deletePaper/{id}")
public String deletePaperById(@PathVariable("id") Integer id,Model model){
paperService.deletePaperById(id);
return "redirect:/paper/getAllPaper";
}
//去往试题管理页面
@RequestMapping("/toManagerQuestion/{id}")
public String toManagerQuestion(@PathVariable("id") Integer id,Model model){
List<QuestionPaper> questionPapers = paperService.paperQueryALlQuestionById(id);
model.addAttribute("papid",id);
Paper paperName=paperService.queryPaperNameById(id);
model.addAttribute("paperName",paperName.getPaperName());
model.addAttribute("questionPapers",questionPapers);
return "paper/ManagerQuestion";
}
//来到试题显示页面为试卷添加试题
@RequestMapping("/toAddQuestion/{id}")
public String getAllQuestion(@PathVariable("id") Integer paperId,Question question,@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "4") int pageSize,
Model model){
//查找所有题目课程和所有类型,且去重
List<Question> questionCourses=questionService.queryAllCourse();
questionCourses.add(new Question("bug","all"));
List<Question> questionTypes=questionService.queryAllType();
questionTypes.add(new Question("k","bug"));
String questionCourseBef = question.getQuestionCourse();
String questionCourseresRes="";
if(questionCourseBef==null){
//默认查询所有
questionCourseresRes="all";
}else {
questionCourseresRes=questionCourseBef;
}
//若是第一次查询则用上次提交的表单中的类型、课程,若是第二次查询则延用上次类型
String questionTypeBef=question.getQuestionType();
String questionTypesRes="";
if(questionTypeBef==null){
//默认查询所有
questionTypesRes="k";
}else {
questionTypesRes=questionTypeBef;
}
//查询试卷中已存在的试题,不能再次被添加
List<Question> questionids=paperService.queryALlQuestionIdInPaperById(paperId);
List<Integer> quesds=new ArrayList<>();
if(questionids!=null){
for(Question qid:questionids){
quesds.add(qid.getQuestionId());
}
}
model.addAttribute("quesds",quesds);
//分页
PageHelper.startPage(pageNum,pageSize);//这行是重点,表示从pageNum页开始,每页pageSize条数据
List<Question> questions = questionService.getAll(question);
PageInfo<Question> pageInfo = new PageInfo<Question>(questions);
Paper paperName=paperService.queryPaperNameById(paperId);
model.addAttribute("paperName",paperName.getPaperName());
model.addAttribute("questionCourseresRes",questionCourseresRes);
model.addAttribute("questionTypesRes",questionTypesRes);
model.addAttribute("questionTypes",questionTypes);
model.addAttribute("questionCourses",questionCourses);
model.addAttribute("pageInfo",pageInfo);
model.addAttribute("paperId",paperId);
return "paper/AddQuestion";
}
//为试卷添加试题
@RequestMapping("/AddQuestion")
public String AddQuestion(Integer paperId,Integer quesId,Integer pageNum,String questionCourse,String questionType){
QuestionPaper questionPaper=new QuestionPaper(quesId,paperId);
paperService.AddQuestionToPaperById(questionPaper);
return "redirect:/paper/toAddQuestion/"+paperId+"?pageNum="+pageNum+"&questionCourse="+questionCourse+"&questionType="+questionType;
}
//从试卷中移除试题
@RequestMapping("/detachQuestion")
public String detachQuestion(Integer qpId,Integer paperId){
paperService.detachQuestionById(qpId);
return "redirect:/paper/toManagerQuestion/"+paperId;
}
//去往随机组题页面
@RequestMapping("/toRandomQuestion/{id}")
public String toRandomQuestion(@PathVariable ("id") Integer papid,Question question,Model model){
List<Question> questionCourses=questionService.queryAllCourse();
questionCourses.add(new Question("bug","all"));
//查找所有题目课程和所有类型,且去重
//用于条件查询题库中尚未分配的题有多少道
int TotalQuestionNums=questionService.queryAllQuestionNums();
List<Map> maps = questionService.queryNumOfQuestionType();
List<String> course=new ArrayList<>();
List<Integer> count=new ArrayList<>();
for (Map map:maps){
for (Object key : map.keySet()) {
if(map.get(key) instanceof String){
course.add(map.get(key).toString());
}else{
count.add(Integer.parseInt(map.get(key).toString()));
}
}
}
Paper paperName=paperService.queryPaperNameById(papid);
model.addAttribute("paperName",paperName.getPaperName());
model.addAttribute("count",count);
model.addAttribute("course",course);
model.addAttribute("TotalQuestionNums",TotalQuestionNums);
model.addAttribute("paperId",papid);
model.addAttribute("questionCourses",questionCourses);
return "paper/RandomQuestion";
}
//指定试卷id和课程随机生成试题
@RequestMapping("/RandomADDQuestion/{id}")
public String RandomADDQuestion(@PathVariable ("id") Integer papid,String questionCourse,int QuesNums,Model model){
try{
//找到试卷所有未分配的试题
PapIdQuesCourse papIdQuesCourse=new PapIdQuesCourse();
papIdQuesCourse.setPapid(papid);
papIdQuesCourse.setQuestionCourse(questionCourse);
List<Question> questionsNodivIds=questionService.queryAllQueIdNotInPaperById(papIdQuesCourse);
List<Integer> list=new ArrayList<>();
for(Question question:questionsNodivIds){
list.add(question.getQuestionId());
}
Collections.shuffle(list);
List<QuestionPaper> questionPapers=new ArrayList<>();
for(int i=0;i<QuesNums;i++){
QuestionPaper questionPaper=new QuestionPaper(list.get(i),papid);
questionPapers.add(questionPaper);
}
int nums=questionService.AddAllQues(questionPapers);
}catch (Exception e){
model.addAttribute("papid",papid);
return "paper/error";
}
return "redirect:/paper/toManagerQuestion/"+papid;
}
@RequestMapping("/deleteAllQues/{id}")
public String deleteAllQues(@PathVariable ("id") Integer papid,Integer []ques){
int nums=questionService.queryIdByPapQue(ques);
return "redirect:/paper/toManagerQuestion/"+papid;
}
}
package com.zhao.quiz.controller;
import com.zhao.quiz.domain.*;
import com.zhao.quiz.service.ExamService;
import com.zhao.quiz.service.PaperService;
import com.zhao.quiz.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("/exam")
public class ExamController {
@Autowired
private ExamService examService;
@Autowired
private PaperService paperService;
@Autowired
private RecordService recordService;
//前台跳转
@RequestMapping("/toExam")
public String toExam(Model model){
List<Exam> Exams = examService.getAll();
model.addAttribute("Exams",Exams);
return "exam/examplan";
}
@RequestMapping("/toHist/{id}")
public String toHist(@PathVariable ("id") Integer id,Model model){
List<Record> records=recordService.queryAllExamById(id);
model.addAttribute("records",records);
return "exam/histplan";
}
//从其他页面跳转到home
@RequestMapping("/toHome")
public String tohome(){
return "redirect:/indexprexam";
}
//来到对应考试页面
@RequestMapping("/toDoExam/{id}")
public String toDoExam(@PathVariable ("id") Integer id,Model model,String examId){
List<QuestionPaper> questionPapers = paperService.paperQueryALlQuestionByIdOrderByType(id);
int exId=Integer.parseInt(examId);
Exam examById = examService.getExamById(exId);
Paper paperName = paperService.queryPaperNameById(examById.getPaperId());
model.addAttribute("paperName",paperName);
model.addAttribute("examById",examById);
model.addAttribute("questionPapers",questionPapers);
return "exam/doExam";
}
//提交试卷
@RequestMapping("/submitExam")
public String submitExam(Integer paperId, Integer studentId, HttpServletRequest request){
List<QuestionPaper> questionPapers = paperService.paperQueryALlQuestionByIdOrderByType(paperId);
List<String> ans=new ArrayList<>();
List<String> RightAns=new ArrayList<>();
for (QuestionPaper qb:questionPapers){
RightAns.add(qb.getQuestion().getQuestionOpright());
String parameter="";
String []parameters;
if(qb.getQuestion().getQuestionType().equals("y")){
parameters= request.getParameterValues("optionsSelect" + qb.getQuestionId());
for(String s:parameters){
parameter+=s;
}
}else {
parameter = request.getParameter("optionsSelect" + qb.getQuestionId());
}
ans.add(parameter);
}
//核对答案得到成绩
int k=0; //哨兵
Double y=0.0; //正确数
int score=0; //得分
int a=0; //记录单选题个数
int b=0; //记录多选题个数
int c=0; //记录判断题个数
int totalScore=0;
for (QuestionPaper qb:questionPapers){
//若为单选题则正确+单选题分数
if(qb.getQuestion().getQuestionType().equals("x")){
if(ans.get(k).equals(RightAns.get(k))){
score+=qb.getPaper().getScoreSin();
y++;
}
a++;
k++;
}else if(qb.getQuestion().getQuestionType().equals("y")){
if(ans.get(k).equals(RightAns.get(k))){
score+=qb.getPaper().getScoreChe();
y++;
}
b++;
k++;
}else {
if(ans.get(k).equals(RightAns.get(k))){
score+=qb.getPaper().getScoreJug();
y++;
}
c++;
k++;
}
}
int scoreSin1 = questionPapers.get(0).getPaper().getScoreSin();
int scoreChe1 = questionPapers.get(0).getPaper().getScoreChe();
int scoreJug1 = questionPapers.get(0).getPaper().getScoreJug();
int bool=recordService.queryBooleanToscore(paperId);
if (bool==0){
totalScore=scoreSin1*a+scoreChe1*b+scoreJug1*c; //得到每张试卷总分
Toscore toscore=new Toscore();
toscore.setPaperId(paperId);
toscore.setToscore(totalScore);
recordService.AddToScore(toscore);
}
//保存答题记录
String answer = String.join(",", ans);
Paper paper = paperService.queryPaperNameById(paperId);
String paperName = paper.getPaperName();
Double recordAcc=y/k;
int recordScore=score;
Record record=new Record();
record.setRecordName(paperName);
record.setStudentId(studentId);
record.setPaperId(paperId);
record.setRecordAnswer(answer);
record.setRecordAcc(recordAcc);
record.setRecordScore(recordScore);
recordService.addRecord(record);
return "redirect:/exam/toExam";
}
/**
* 考试后台
* */
//查看所有考试安排后台
@RequestMapping("/getAllExam")
public String getAllExam(Model model){
List<Exam> Exams = examService.getAllS();
model.addAttribute("Exams",Exams);
return "exam/backexamlist";
}
//去往考试添加页面
@RequestMapping("/toAddExam")
public String toAddExam(Model model){
List<Paper> papers = paperService.getAll();
model.addAttribute("papers",papers);
return "exam/AddExam";
}
//添加操作
@RequestMapping("/addExam")
public String addExam(Exam exam, String examBegins,String examEnds) throws ParseException {
String t1 = examBegins.replace("T", " ");
String t2 = examEnds.replace("T", " ");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date begin = sdf.parse(t1);
Date end = sdf.parse(t2);
exam.setExamBegin(begin);
exam.setExamEnd(end);
examService.AddExam(exam);
return "redirect:/exam/getAllExam";
}
@RequestMapping("/deleteExam/{id}")
public String toEditExam(@PathVariable ("id") Integer id,Model model){
examService.deleteById(id);
return "redirect:/exam/getAllExam";
}
}