「这是我参与2022首次更文挑战的第26天,活动详情查看:2022首次更文挑战」
这一篇主要讲考试系统试题,试卷这些模块的功能的拓展。
计算每道题目的得分
1、questionDao.getQuestion(questionId)查询题目信息
2、if(userAnswer.equals(question.getAnswer()))判断如果考生的答案,与题目的正确答案一致的话,就给予分数,否则就是0分
3、if("1".equals(type)){ return 20; }else{ return 30; }判断题目类型,单选题为20分,多选题为30分
private int calScore(String questionId,String userAnswer,String type)throws Exception{
Question question=questionDao.getQuestion(questionId);
if(userAnswer.equals(question.getAnswer())){
if("1".equals(type)){
return 20;
}else{
return 30;
}
}else{
return 0;
}
}
获取所有考试成绩
1、StringUtil.isEmpty(page)判断前端传过来的page是否为空,如果是空的,则为首页。
2、session.setAttribute("s_exam", s_exam);和session.getAttribute("s_exam");都是获取Exam对象信息,并最后设置Exam信息。
3、examDao.getExams(s_exam,pageBean);查询考试信息。
4、pageCode=PageUtil.genPagation(request.getContextPath()+"/exam!examList",total, Integer.parseInt(page),设置分页大小,总数,分页信息。
5、mainPage="exam/examList.jsp";返回状态为success,并跳转到指点页面。
public String examList()throws Exception{
HttpSession session=request.getSession();
if(StringUtil.isEmpty(page)){
page="1";
}
if(s_exam!=null){
session.setAttribute("s_exam", s_exam);
}else{
Object o=session.getAttribute("s_exam");
if(o!=null){
s_exam=(Exam)o;
}else{
s_exam=new Exam();
}
}
PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(PropertiesUtil.getValue("pageSize")));
examList=examDao.getExams(s_exam,pageBean);
total=examDao.examCount(s_exam);
pageCode=PageUtil.genPagation(request.getContextPath()+"/exam!examList",total, Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue("pageSize")));
mainPage="exam/examList.jsp";
return SUCCESS;
}
获取考试信息
1、HibernateUtil.getSessionFactory().getCurrentSession()通过Hibernate的工具类,得到会话工厂,获得当前会话。
2、session.beginTransaction();开始业务。
3、StringBuffer hql开始拼接sql语句。
4、首先判断考生id存不存在,即有没有该考生,有的话,拼接这个模糊查询语句。
5、接着判断考生姓名存不存在,有的话,sql语句带上考生姓名。
6、通过上面两个判断,可以知道,获取考生信息,是带了考生id,考生姓名这两个条件作为模糊查询的条件。
7、List<Exam> examList=(List<Exam>)query.list(); session.getTransaction().commit();拼接完sql语句,就进行执行sql语句,并返回结果。
public List<Exam> getExams(Exam s_exam,PageBean pageBean)throws Exception{
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
StringBuffer hql=new StringBuffer("from Exam exam");
if(s_exam.getStudent()!=null&&StringUtil.isNotEmpty(s_exam.getStudent().getId())){
hql.append(" and exam.student.id like '%"+s_exam.getStudent().getId()+"%'");
}
if(s_exam.getStudent()!=null&&StringUtil.isNotEmpty(s_exam.getStudent().getName())){
hql.append(" and exam.student.name like '%"+s_exam.getStudent().getName()+"%'");
}
Query query=session.createQuery(hql.toString().replaceFirst("and", "where"));
if(pageBean!=null){
query.setFirstResult(pageBean.getStart());
query.setMaxResults(pageBean.getPageSize());
}
@SuppressWarnings("unchecked")
List<Exam> examList=(List<Exam>)query.list();
session.getTransaction().commit();
return examList;
}