package com.java.yzl.action;
import com.java.yzl.bean.Manager;
import com.java.yzl.dao.ManagerDao;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class ManagerAction extends ActionSupport implements ServletRequestAware {
/**
*/
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private ManagerDao managerDao = new ManagerDao();
private Manager manager;
private String error;
public Manager getManager() {
return manager;
}
public void setManager(Manager manager) {
this.manager = manager;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
/**
-
登录验证
-
@return
-
@throws Exception
*/
public String login() throws Exception {
HttpSession session = request.getSession();
Manager currentUser = managerDao.login(manager);
if (currentUser == null) {
error = “用户名或者密码错误!”;
return ERROR;
} else {
session.setAttribute(“currentUser”, currentUser);
return SUCCESS;
}
}
/**
-
注销用户
-
@throws Exception
*/
public String logout() throws Exception {
request.getSession().invalidate();
return “logout”;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
PaperAction
package com.java.yzl.action;
import com.java.yzl.bean.Paper;
import com.java.yzl.bean.Question;
import com.java.yzl.dao.PaperDao;
import com.java.yzl.dao.QuestionDao;
import com.java.yzl.util.ResponseUtil;
import com.java.yzl.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import java.util.*;
/**
-
试卷Action类
-
@author Administrator
*/
public class PaperAction extends ActionSupport {
/**
*/
private static final long serialVersionUID = 1L;
private PaperDao paperDao = new PaperDao();
private QuestionDao questionDao = new QuestionDao();
private String mainPage;
private String paperId;
private List paperList = new ArrayList();
private List squestionList = new ArrayList();
private List mquestionList = new ArrayList();
private String title; // 标题
private Paper paper;
public List getPaperList() {
return paperList;
}
public void setPaperList(List paperList) {
this.paperList = paperList;
}
public List getSquestionList() {
return squestionList;
}
public void setSquestionList(List squestionList) {
this.squestionList = squestionList;
}
public List getMquestionList() {
return mquestionList;
}
public void setMquestionList(List mquestionList) {
this.mquestionList = mquestionList;
}
public Paper getPaper() {
return paper;
}
public void setPaper(Paper paper) {
this.paper = paper;
}
public String getPaperId() {
return paperId;
}
public void setPaperId(String paperId) {
this.paperId = paperId;
}
public String getMainPage() {
return mainPage;
}
public void setMainPage(String mainPage) {
this.mainPage = mainPage;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
/**
-
获取所有试卷
-
@return
-
@throws Exception
*/
public String list() throws Exception {
paperList = paperDao.getPapers();
mainPage = “exam/selectPaper.jsp”;
return SUCCESS;
}
/**
-
获取所有试卷(管理)
-
@return
-
@throws Exception
*/
public String paperList() throws Exception {
paperList = paperDao.getPapers();
mainPage = “paper/paperList.jsp”;
return SUCCESS;
}
/**
-
通过id获取试卷实体
-
@return
-
@throws Exception
*/
public String getPaperById() throws Exception {
paper = paperDao.getPaper(paperId);
mainPage = “paper/paperSave.jsp”;
return SUCCESS;
}
/**
-
保存预操作
-
@return
-
@throws Exception
*/
public String preSave() throws Exception {
if (StringUtil.isNotEmpty(paperId)) {
paper = paperDao.getPaper(paperId);
title = “修改试卷”;
} else {
title = “添加试卷”;
}
mainPage = “paper/paperSave.jsp”;
return SUCCESS;
}
/**
-
保存试卷
-
@return
-
@throws Exception
*/
public String savePaper() throws Exception {
if (StringUtil.isNotEmpty(paperId)) {
paper.setId(Integer.parseInt(paperId));
} else {
paper.setJoinDate(new Date());
}
paperDao.savePaper(paper);
return “save”;
}
/**
-
删除试卷
-
@return
-
@throws Exception
*/
public String deletePaper() throws Exception {
paper = paperDao.getPaper(paperId);
JSONObject resultJson = new JSONObject();
if (questionDao.existQuestionByPaperId(paperId)) {
resultJson.put(“error”, “试卷下面有题目,不能删除”);
} else {
paperDao.paperDelete(paper);
resultJson.put(“success”, true);
}
ResponseUtil.write(resultJson, ServletActionContext.getResponse());
return null;
}
/**
-
获取指定试卷
-
@return
-
@throws Exception
*/
public String getDetailPaper() throws Exception {
paper = paperDao.getPaper(paperId);
Set questionList = paper.getQuestions();
Iterator it = questionList.iterator();
while (it.hasNext()) {
Question q = it.next();
if (“1”.equals(q.getType())) {
squestionList.add(q);
} else {
mquestionList.add(q);
}
}
squestionList = this.getRandomQuestion(squestionList, 3);
mquestionList = this.getRandomQuestion(mquestionList, 2);
mainPage = “exam/paper.jsp”;
return SUCCESS;
}
/**
-
获取随机试题
-
@param questionList
-
@param num
-
@return
*/
private List getRandomQuestion(List questionList, int num) {
List resultList = new ArrayList();
Random random = new Random();
if (num > 0) {
for (int i = 1; i <= num; i++) {
int n = random.nextInt(questionList.size());
Question q = questionList.get(n);
if (resultList.contains(q)) {
i–;
} else {
resultList.add(questionList.get(n));
}
}
}
return resultList;
}
}
QuestionAction
package com.java.yzl.action;
import com.java.yzl.bean.PageBean;
import com.java.yzl.bean.Paper;
import com.java.yzl.bean.Question;
import com.java.yzl.dao.PaperDao;
import com.java.yzl.dao.QuestionDao;
import com.java.yzl.util.PageUtil;
import com.java.yzl.util.PropertiesUtil;
import com.java.yzl.util.ResponseUtil;
import com.java.yzl.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
public class QuestionAction extends ActionSupport implements ServletRequestAware {
/**
*/
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private QuestionDao questionDao = new QuestionDao();
private PaperDao paperDao = new PaperDao();
private List questionList;
private List paperList;
private String mainPage;
private String questionId;
private Question question;
private String title;
private String page;
private int total;
private String pageCode;
private Question s_question;
public List getPaperList() {
return paperList;
}
public void setPaperList(List paperList) {
this.paperList = paperList;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List getQuestionList() {
return questionList;
}
public void setQuestionList(List questionList) {
this.questionList = questionList;
}
public String getMainPage() {
return mainPage;
}
public void setMainPage(String mainPage) {
this.mainPage = mainPage;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String getPageCode() {
return pageCode;
}
public void setPageCode(String pageCode) {
this.pageCode = pageCode;
}
public Question getS_question() {
return s_question;
}
public void setS_question(Question s_question) {
this.s_question = s_question;
}
public String getQuestionId() {
return questionId;
}
public void setQuestionId(String questionId) {
this.questionId = questionId;
}
public Question getQuestion() {
return question;
}
public void setQuestion(Question question) {
this.question = question;
}
/**
-
查询试题信息
-
@return
-
@throws Exception
*/
public String list() throws Exception {
HttpSession session = request.getSession();
if (StringUtil.isEmpty(page)) {
page = “1”;
}
if (s_question != null) {
session.setAttribute(“s_question”, s_question);
} else {
Object o = session.getAttribute(“s_question”);
if (o != null) {
s_question = (Question) o;
} else {
s_question = new Question();
}
}
PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue(“pageSize”)));
questionList = questionDao.getQuestions(s_question, pageBean);
total = questionDao.questionCount(s_question);
pageCode = PageUtil.genPagation(request.getContextPath() + “/question!list”, total, Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue(“pageSize”)));
mainPage = “question/questionList.jsp”;
return SUCCESS;
}
/**
-
通过id获取试题
-
@return
-
@throws Exception
*/
public String getQuestionById() throws Exception {
question = questionDao.getQuestion(questionId);
mainPage = “question/questionShow.jsp”;
return SUCCESS;
}
/**
-
预编辑操作
-
@return
-
@throws Exception
*/
public String preSave() throws Exception {
paperList = paperDao.getPapers();
if (StringUtil.isNotEmpty(questionId)) {
question = questionDao.getQuestion(questionId);
title = “修改试题信息”;
} else {
title = “添加试题信息”;
}
mainPage = “question/questionSave.jsp”;
return SUCCESS;
}
/**
-
删除试题
-
@return
-
@throws Exception
*/
public String delete() throws Exception {
question = questionDao.getQuestion(questionId);
questionDao.deleteQuestion(question);
JSONObject resultJson = new JSONObject();
resultJson.put(“success”, true);
ResponseUtil.write(resultJson, ServletActionContext.getResponse());
return null;
}
/**
-
保存试题
-
@return
-
@throws Exception
*/
public String saveQuestion() throws Exception {
if (StringUtil.isNotEmpty(questionId)) {
question.setId(Integer.parseInt(questionId));
}
questionDao.saveQuestion(question);
return “save”;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
StudentAction
package com.java.yzl.action;
import com.java.yzl.bean.PageBean;
import com.java.yzl.bean.Student;
import com.java.yzl.dao.StudentDao;
import com.java.yzl.util.*;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
/**
-
学生Action类
-
@author Administrator
*/
public class StudentAction extends ActionSupport implements ServletRequestAware {
/**
*/
private static final long serialVersionUID = 1L;
private StudentDao studentDao = new StudentDao();
private HttpServletRequest request;
private String mainPage;
private Student student;
private String error;
private String page;
private int total;
private String pageCode;
private List studentList;
private Student s_student;
private String id; // 学生编号
private String title; // 标题
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public String getMainPage() {
return mainPage;
}
public void setMainPage(String mainPage) {
this.mainPage = mainPage;
}
public String getPageCode() {
return pageCode;
}
public void setPageCode(String pageCode) {
this.pageCode = pageCode;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public Student getS_student() {
return s_student;
}
public void setS_student(Student s_student) {
this.s_student = s_student;
}
public List getStudentList() {
return studentList;
}
public void setStudentList(List studentList) {
this.studentList = studentList;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
/**
-
登录验证
-
@return
-
@throws Exception
*/
public String login() throws Exception {
HttpSession session = request.getSession();
Student currentUser = studentDao.login(student);
if (currentUser == null) {
error = “准考证号或者密码错误!”;
return ERROR;
} else {
session.setAttribute(“currentUser”, currentUser);
return SUCCESS;
}
}
/**
-
修改密码预操作
-
@return
-
@throws Exception
*/
public String preUpdatePassword() throws Exception {
mainPage = “student/updatePassword.jsp”;
return SUCCESS;
}
/**
-
修改密码
-
@return
-
@throws Exception
*/
public String updatePassword() throws Exception {
Student s = studentDao.getStudentById(student.getId());
s.setPassword(student.getPassword());
studentDao.saveStudent(s);
mainPage = “student/updateSuccess.jsp”;
return SUCCESS;
}
/**
-
查询学生信息
-
@return
-
@throws Exception
*/
public String list() throws Exception {
HttpSession session = request.getSession();
if (StringUtil.isEmpty(page)) {
page = “1”;
}
if (s_student != null) {
session.setAttribute(“s_student”, s_student);
} else {
Object o = session.getAttribute(“s_student”);
if (o != null) {
s_student = (Student) o;
} else {
s_student = new Student();
}
}
PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue(“pageSize”)));
studentList = studentDao.getStudents(s_student, pageBean);
total = studentDao.studentCount(s_student);
pageCode = PageUtil.genPagation(request.getContextPath() + “/student!list”, total, Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue(“pageSize”)));
mainPage = “student/studentList.jsp”;
return SUCCESS;
}
/**
-
获取学生
-
@return
-
@throws Exception
*/
public String getStudentById() throws Exception {
student = studentDao.getStudent(id);
mainPage = “student/studentSave.jsp”;
return SUCCESS;
}
/**
-
保存学生
-
@return
-
@throws Exception
*/
public String saveStudent() throws Exception {
if (StringUtil.isEmpty(student.getId())) {
student.setId(“JS” + DateUtil.getCurrentDateStr());
}
studentDao.saveStudent(student);
return “save”;
}
/**
-
删除学生
-
@return
-
@throws Exception
*/
public String deleteStudent() throws Exception {
student = studentDao.getStudent(id);
studentDao.studentDelete(student);
JSONObject resultJson = new JSONObject();
resultJson.put(“success”, true);
ResponseUtil.write(resultJson, ServletActionContext.getResponse());
return null;
}
/**
-
预添加操作
-
@return
-
@throws Exception
*/
public String preSave() throws Exception {
if (StringUtil.isNotEmpty(id)) {
student = studentDao.getStudent(id);
title = “修改学生信息”;
} else {
title = “添加学生信息”;
}
mainPage = “student/studentSave.jsp”;
return SUCCESS;
}
/**
-
注销用户
-
@throws Exception
*/
public String logout() throws Exception {
request.getSession().invalidate();
return “logout”;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
ExamDao
package com.java.yzl.dao;
import com.java.yzl.bean.Exam;
import com.java.yzl.bean.PageBean;
import com.java.yzl.daoInter.ExamDaoInter;
import com.java.yzl.util.HibernateUtil;
import com.java.yzl.util.StringUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import java.math.BigInteger;
import java.util.List;
public class ExamDao implements ExamDaoInter {
// 保存考试信息
public void saveExam(Exam exam) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.merge(exam);
session.getTransaction().commit();
}
//获取考试信息
public List 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 examList = (List) query.list();
session.getTransaction().commit();
return examList;
}
//查询考试信息记录数
public int examCount(Exam s_exam) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
StringBuffer sql = new StringBuffer("select count(*) from t_exam t1 ,t_student t2 where t1.studentId=t2.id ");
if (s_exam.getStudent() != null && StringUtil.isNotEmpty(s_exam.getStudent().getId())) {
sql.append(" and t2.id like ‘%" + s_exam.getStudent().getId() + "%’");
}
if (s_exam.getStudent() != null && StringUtil.isNotEmpty(s_exam.getStudent().getName())) {
sql.append(" and t2.name like ‘%" + s_exam.getStudent().getName() + "%’");
}
Query query = session.createSQLQuery(sql.toString());
int count = ((BigInteger) query.uniqueResult()).intValue();
session.getTransaction().commit();
return count;
}
}
ManagerDao
package com.java.yzl.dao;
import com.java.yzl.bean.Manager;
import com.java.yzl.daoInter.ManagerDaoInter;
import com.java.yzl.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
public class ManagerDao implements ManagerDaoInter {
//管理员登录验证
public Manager login(Manager manager) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery("from Manager as m where m.userName=:userName and m.password=:password ");
query.setString(“userName”, manager.getUserName());
query.setString(“password”, manager.getPassword());
Manager resultManager = (Manager) query.uniqueResult();
session.getTransaction().commit();
return resultManager;
}
}
PaperDao
package com.java.yzl.dao;
import com.java.yzl.bean.Paper;
import com.java.yzl.daoInter.PaperDaoInter;
import com.java.yzl.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import java.util.List;
public class PaperDao implements PaperDaoInter {
//获取所有试卷
public List getPapers() throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery(“from Paper”);
@SuppressWarnings(“unchecked”)
List paperList = (List) query.list();
session.getTransaction().commit();
return paperList;
}
//获取指定试卷
public Paper getPaper(String paperId) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Paper paper = (Paper) session.get(Paper.class, Integer.parseInt(paperId));
session.getTransaction().commit();
return paper;
}
//保存试卷实体
public void savePaper(Paper paper) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.merge(paper);
session.getTransaction().commit();
}
//删除试卷
public void paperDelete(Paper paper) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.delete(paper);
session.getTransaction().commit();
}
}
QuestionDao
package com.java.yzl.dao;
//问题DAO类
import com.java.yzl.bean.PageBean;
import com.java.yzl.bean.Question;
import com.java.yzl.bean.Student;
import com.java.yzl.daoInter.QuestionDaoInter;
import com.java.yzl.util.HibernateUtil;
import com.java.yzl.util.StringUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import java.math.BigInteger;
import java.util.List;
public class QuestionDao implements QuestionDaoInter {
//通过问题id获取问题实体
public Question getQuestion(String questionId) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Question question = (Question) session.get(Question.class, Integer.parseInt(questionId));
session.getTransaction().commit();
return question;
}
//判断执行的试卷下有无题目
public boolean existQuestionByPaperId(String paperId) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery(“from Question as q where q.paper.id=:paperId”);
query.setString(“paperId”, paperId);
@SuppressWarnings(“unchecked”)
List studentList = (List) query.list();
session.getTransaction().commit();
if (studentList.size() > 0) {
return true;
} else {
return false;
}
}
//获取所有题目
public List getQuestions(Question s_question, PageBean pageBean) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
StringBuffer hql = new StringBuffer(“from Question”);
if (StringUtil.isNotEmpty(s_question.getSubject())) {
hql.append(" and subject like ‘%" + s_question.getSubject() + "%’");
}
Query query = session.createQuery(hql.toString().replaceFirst(“and”, “where”));
if (pageBean != null) {
query.setFirstResult(pageBean.getStart());
query.setMaxResults(pageBean.getPageSize());
}
@SuppressWarnings(“unchecked”)
List questionList = (List) query.list();
session.getTransaction().commit();
return questionList;
}
//查询试题记录数
public int questionCount(Question s_question) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
StringBuffer sql = new StringBuffer(“select count(*) from t_question”);
if (StringUtil.isNotEmpty(s_question.getSubject())) {
sql.append(" and subject like ‘%" + s_question.getSubject() + "%’");
}
Query query = session.createSQLQuery(sql.toString().replaceFirst(“and”, “where”));
int count = ((BigInteger) query.uniqueResult()).intValue();
session.getTransaction().commit();
return count;
}
//保存试题实体
public void saveQuestion(Question question) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.merge(question);
session.getTransaction().commit();
}
//删除试题
public void deleteQuestion(Question question) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.delete(question);
session.getTransaction().commit();
}
}
StudentDao
package com.java.yzl.dao;
import com.java.yzl.bean.PageBean;
import com.java.yzl.bean.Student;
import com.java.yzl.daoInter.StudentDaoInter;
import com.java.yzl.util.HibernateUtil;
import com.java.yzl.util.StringUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import java.math.BigInteger;
import java.util.List;
public class StudentDao implements StudentDaoInter {
//学生登录
public Student login(Student student) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery("from Student as s where s.id=:id and s.password=:password ");
query.setString(“id”, student.getId());
query.setString(“password”, student.getPassword());
Student resultStu = (Student) query.uniqueResult();
session.getTransaction().commit();
return resultStu;
}
//通过id获取学生实体
public Student getStudentById(String id) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Student student = (Student) session.get(Student.class, id);
session.getTransaction().commit();
return student;
}
//保存学生实体
public void saveStudent(Student student) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.merge(student);
session.getTransaction().commit();
}
//获取所有学生
public List getStudents(Student s_student, PageBean pageBean) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
StringBuffer hql = new StringBuffer(“from Student”);
if (StringUtil.isNotEmpty(s_student.getId())) {
hql.append(" and id like ‘%" + s_student.getId() + "%’");
}
if (StringUtil.isNotEmpty(s_student.getName())) {
hql.append(" and name like ‘%" + s_student.getName() + "%’");
}
Query query = session.createQuery(hql.toString().replaceFirst(“and”, “where”));
if (pageBean != null) {
query.setFirstResult(pageBean.getStart());
query.setMaxResults(pageBean.getPageSize());
}
@SuppressWarnings(“unchecked”)
最后
我还为大家准备了一套体系化的架构师学习资料包以及BAT面试资料,供大家参考及学习
已经将知识体系整理好(源码,笔记,PPT,学习视频)
DaoInter;
import com.java.yzl.util.HibernateUtil;
import com.java.yzl.util.StringUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import java.math.BigInteger;
import java.util.List;
public class StudentDao implements StudentDaoInter {
//学生登录
public Student login(Student student) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery("from Student as s where s.id=:id and s.password=:password ");
query.setString(“id”, student.getId());
query.setString(“password”, student.getPassword());
Student resultStu = (Student) query.uniqueResult();
session.getTransaction().commit();
return resultStu;
}
//通过id获取学生实体
public Student getStudentById(String id) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Student student = (Student) session.get(Student.class, id);
session.getTransaction().commit();
return student;
}
//保存学生实体
public void saveStudent(Student student) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.merge(student);
session.getTransaction().commit();
}
//获取所有学生
public List getStudents(Student s_student, PageBean pageBean) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
StringBuffer hql = new StringBuffer(“from Student”);
if (StringUtil.isNotEmpty(s_student.getId())) {
hql.append(" and id like ‘%" + s_student.getId() + "%’");
}
if (StringUtil.isNotEmpty(s_student.getName())) {
hql.append(" and name like ‘%" + s_student.getName() + "%’");
}
Query query = session.createQuery(hql.toString().replaceFirst(“and”, “where”));
if (pageBean != null) {
query.setFirstResult(pageBean.getStart());
query.setMaxResults(pageBean.getPageSize());
}
@SuppressWarnings(“unchecked”)
最后
我还为大家准备了一套体系化的架构师学习资料包以及BAT面试资料,供大家参考及学习
已经将知识体系整理好(源码,笔记,PPT,学习视频)
[外链图片转存中…(img-r91aAqPg-1714298950344)]
[外链图片转存中…(img-6XYqooE5-1714298950345)]
[外链图片转存中…(img-OvuN5dBm-1714298950345)]