JavaWeb项目开发案例精粹-第3章在线考试系统-003Dao层

1.

 1 package com.sanqing.dao;
 2 
 3 import java.util.List;
 4 
 5 import com.sanqing.po.Student;
 6 
 7 public interface StudentDAO {
 8     public Student findByStudentID(String studentID);//查询方法,根据学生ID查询
 9     public void updateStudent(Student student);//更新学生信息
10     public List<Student> findByStudentName(String studentName);//根据学生姓名查找学生
11     public List<Student> findByStudentClass(String sclass);//根据班级查找学生
12 }

 

2.

 1 package com.sanqing.dao;
 2 
 3 import java.util.Iterator;
 4 import java.util.List;
 5 
 6 import org.hibernate.Query;
 7 import org.hibernate.Session;
 8 import org.hibernate.Transaction;
 9 
10 import com.sanqing.hibernate.HibernateSessionFactory;
11 import com.sanqing.po.Student;
12 import com.sanqing.po.Subject;
13 
14 public class StudentDAOImpl implements StudentDAO{
15     public Student findByStudentID(String studentID) {
16         Session session = HibernateSessionFactory.getSession();//获得Session对象
17         Student student = (Student) session.get(Student.class, studentID);
18         HibernateSessionFactory.closeSession();//关闭Session对象
19         return student;
20     }
21 
22     public void updateStudent(Student student) {
23         Session session = HibernateSessionFactory.getSession();//获得Session对象
24         Transaction  transaction = null;//声明一个事务对象
25         try{
26             transaction = session.beginTransaction();//开启事务
27             session.update(student);//更新学生信息
28             transaction.commit();//提交事务
29         }catch(Exception ex) {
30             ex.printStackTrace();
31             transaction.rollback();//事务回滚
32         }
33         HibernateSessionFactory.closeSession();//关闭Session对象
34     }
35 
36     public List<Student> findByStudentName(String studentName) {
37         Session session = HibernateSessionFactory.getSession();//获得Session对象
38         Query query = session.createQuery("from Student as stu where stu.studentName = ?");
39         query.setString(0, studentName);
40         List list = query.list();                    //查询结果保存到list中
41         HibernateSessionFactory.closeSession();        //关闭Session对象
42         return list;
43     }
44 
45     public List<Student> findByStudentClass(String sclass) {
46         Session session = HibernateSessionFactory.getSession();//获得Session对象
47         Query query = session.createQuery("from Student as stu where stu.sclass = ?");
48         query.setString(0, sclass);
49         List list = query.list();                    //查询结果保存到list中
50         HibernateSessionFactory.closeSession();        //关闭Session对象
51         return list;
52     }
53 }

 

3.

 1 package com.sanqing.dao;
 2 
 3 import java.util.List;
 4 
 5 import com.sanqing.po.Subject;
 6 import com.sanqing.util.Page;
 7 
 8 public interface SubjectDAO {
 9     public void addSubject(Subject subject);//保存方法,用来保存试题
10     public Subject findSubjectByTitle(String subjectTitle);//根据试题标题查找试题
11     public List<Subject> findSubjectByPage(Page page);//分页查询试题
12     public int findSubjectCount();//查询试题总量
13     public Subject findSubjectByID(int subjectID);//根据试题ID查找试题
14     public void updateSubject(Subject subject);//更新方法,用来更新试题
15     public void deleteSubject(int subjectID);//根据试题ID删除试题
16     public List<Subject> likeQueryByTitle(String subjectTitle,Page page);//根据试题标题模糊查询试题
17     public int findLinkQueryCount(String subjectTitle);//查询模糊记录数
18     public List<Subject> randomFindSubject(int number);//随时取出记录
19 }

 

4.

  1 package com.sanqing.dao;
  2 
  3 import java.util.List;
  4 
  5 import org.hibernate.Query;
  6 import org.hibernate.Session;
  7 import org.hibernate.Transaction;
  8 
  9 import com.sanqing.hibernate.HibernateSessionFactory;
 10 import com.sanqing.po.Student;
 11 import com.sanqing.po.Subject;
 12 import com.sanqing.util.Page;
 13 
 14 public class SubjectDAOImpl implements SubjectDAO{
 15     public void addSubject(Subject subject){
 16         Session session = HibernateSessionFactory.getSession();//获得Session对象
 17         Transaction  transaction = null;//声明一个事务对象
 18         try{
 19             transaction = session.beginTransaction();//开启事务
 20             session.save(subject);//保存试题信息
 21             transaction.commit();//提交事务
 22         }catch(Exception ex) {
 23             ex.printStackTrace();
 24             transaction.rollback();//事务回滚
 25         }
 26         HibernateSessionFactory.closeSession();//关闭Session对象
 27     }
 28 
 29     public Subject findSubjectByTitle(String subjectTitle) {
 30         Session session = HibernateSessionFactory.getSession();//获得Session对象
 31         Query query = session.createQuery("from Subject as sub where sub.subjectTitle = ?");
 32         query.setString(0, subjectTitle);
 33         List list = query.list();                    //查询结果保存到list中
 34         HibernateSessionFactory.closeSession();        //关闭Session对象
 35         if(list.size() == 0) {
 36             return null;                            //返回null
 37         }else {
 38             return (Subject) list.get(0);            //返回第一个试题
 39         }
 40     }
 41 
 42     public List<Subject> findSubjectByPage(Page page) {
 43         Session session = HibernateSessionFactory.getSession();//获得Session对象
 44         Query query = session.createQuery("from Subject");
 45         query.setMaxResults(page.getEveryPage());//设置查询记录数
 46         query.setFirstResult(page.getBeginIndex());//设置查询记录起始位置
 47         List list = query.list();                    //查询结果保存到list中
 48         HibernateSessionFactory.closeSession();//关闭Session对象
 49         return list;
 50     }
 51 
 52     public int findSubjectCount() {
 53         Session session = HibernateSessionFactory.getSession();//获得Session对象
 54         Query query = session.createQuery("from Subject");
 55         List list = query.list();                    //查询结果保存到list中
 56         int count = list.size();
 57         HibernateSessionFactory.closeSession();//关闭Session对象
 58         return count;
 59     }
 60 
 61     public Subject findSubjectByID(int subjectID) {
 62         Session session = HibernateSessionFactory.getSession();//获得Session对象
 63         Subject subject = (Subject) session.get(Subject.class, subjectID);
 64         HibernateSessionFactory.closeSession();        //关闭Session对象
 65         return subject;
 66     }
 67 
 68     public void updateSubject(Subject subject) {
 69         Session session = HibernateSessionFactory.getSession();//获得Session对象
 70         Transaction  transaction = null;//声明一个事务对象
 71         try{
 72             transaction = session.beginTransaction();//开启事务
 73             session.update(subject);//更新试题信息
 74             transaction.commit();//提交事务
 75         }catch(Exception ex) {
 76             ex.printStackTrace();
 77             transaction.rollback();//事务回滚
 78         }
 79         HibernateSessionFactory.closeSession();//关闭Session对象
 80     }
 81 
 82     public void deleteSubject(int subjectID) {
 83         Session session = HibernateSessionFactory.getSession();//获得Session对象
 84         Subject subject = (Subject) session.get(Subject.class, subjectID);
 85         Transaction  transaction = null;//声明一个事务对象
 86         try{
 87             transaction = session.beginTransaction();//开启事务
 88             session.delete(subject);
 89             transaction.commit();//提交事务
 90         }catch(Exception ex) {
 91             ex.printStackTrace();
 92             transaction.rollback();//事务回滚
 93         }
 94         HibernateSessionFactory.closeSession();//关闭Session对象
 95     }
 96 
 97     public List<Subject> likeQueryByTitle(String subjectTitle,Page page) {
 98         Session session = HibernateSessionFactory.getSession();//获得Session对象
 99         Query query = session.createQuery("from Subject as sub where sub.subjectTitle like :title ");
100         query.setString("title","%"+subjectTitle+"%");
101         query.setMaxResults(page.getEveryPage());//设置查询记录数
102         query.setFirstResult(page.getBeginIndex());//设置查询记录起始位置
103         List list = query.list();                    //查询结果保存到list中
104         HibernateSessionFactory.closeSession();//关闭Session对象
105         return list;
106     }
107 
108     public int findLinkQueryCount(String subjectTitle) {
109         Session session = HibernateSessionFactory.getSession();//获得Session对象
110         Query query = session.createQuery("from Subject as sub where sub.subjectTitle like :title ");
111         query.setString("title","%"+subjectTitle+"%");
112         List list = query.list();                    //查询结果保存到list中
113         int count = list.size();
114         HibernateSessionFactory.closeSession();//关闭Session对象
115         return count;
116     }
117 
118     public List<Subject> randomFindSubject(int number) {
119         Session session = HibernateSessionFactory.getSession();//获得Session对象
120         Query query = session.createQuery("from Subject as sub order by rand()");
121         query.setMaxResults(number);//设置查询记录数
122         List list = query.list();                    //查询结果保存到list中
123         HibernateSessionFactory.closeSession();//关闭Session对象
124         return list;
125     }
126 }

 

5.

1 package com.sanqing.dao;
2 
3 import com.sanqing.po.Teacher;
4 
5 public interface TeacherDAO {
6     public Teacher findByTeacherID(String teacherID);//查询方法,根据教师ID查询
7 }

 

6.

 1 package com.sanqing.dao;
 2 
 3 import org.hibernate.Session;
 4 
 5 import com.sanqing.hibernate.HibernateSessionFactory;
 6 import com.sanqing.po.Teacher;
 7 
 8 public class TeacherDAOImpl implements TeacherDAO{
 9     public Teacher findByTeacherID(String teacherID) {
10         Session session = HibernateSessionFactory.getSession();//获得Session对象
11         Teacher teacher = (Teacher) session.get(Teacher.class, teacherID);
12         HibernateSessionFactory.closeSession();//关闭Session对象
13         return teacher;
14     }
15 }

 

posted @ 2016-03-25 17:30  shamgod  阅读(873)  评论(0编辑  收藏  举报
haha