1.

 package com.sanqing.dao;

 import java.util.List;

 import com.sanqing.po.Student;

 public interface StudentDAO {
public Student findByStudentID(String studentID);//查询方法,根据学生ID查询
public void updateStudent(Student student);//更新学生信息
public List<Student> findByStudentName(String studentName);//根据学生姓名查找学生
public List<Student> findByStudentClass(String sclass);//根据班级查找学生
}

2.

 package com.sanqing.dao;

 import java.util.Iterator;
import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction; import com.sanqing.hibernate.HibernateSessionFactory;
import com.sanqing.po.Student;
import com.sanqing.po.Subject; public class StudentDAOImpl implements StudentDAO{
public Student findByStudentID(String studentID) {
Session session = HibernateSessionFactory.getSession();//获得Session对象
Student student = (Student) session.get(Student.class, studentID);
HibernateSessionFactory.closeSession();//关闭Session对象
return student;
} public void updateStudent(Student student) {
Session session = HibernateSessionFactory.getSession();//获得Session对象
Transaction transaction = null;//声明一个事务对象
try{
transaction = session.beginTransaction();//开启事务
session.update(student);//更新学生信息
transaction.commit();//提交事务
}catch(Exception ex) {
ex.printStackTrace();
transaction.rollback();//事务回滚
}
HibernateSessionFactory.closeSession();//关闭Session对象
} public List<Student> findByStudentName(String studentName) {
Session session = HibernateSessionFactory.getSession();//获得Session对象
Query query = session.createQuery("from Student as stu where stu.studentName = ?");
query.setString(0, studentName);
List list = query.list(); //查询结果保存到list中
HibernateSessionFactory.closeSession(); //关闭Session对象
return list;
} public List<Student> findByStudentClass(String sclass) {
Session session = HibernateSessionFactory.getSession();//获得Session对象
Query query = session.createQuery("from Student as stu where stu.sclass = ?");
query.setString(0, sclass);
List list = query.list(); //查询结果保存到list中
HibernateSessionFactory.closeSession(); //关闭Session对象
return list;
}
}

3.

 package com.sanqing.dao;

 import java.util.List;

 import com.sanqing.po.Subject;
import com.sanqing.util.Page; public interface SubjectDAO {
public void addSubject(Subject subject);//保存方法,用来保存试题
public Subject findSubjectByTitle(String subjectTitle);//根据试题标题查找试题
public List<Subject> findSubjectByPage(Page page);//分页查询试题
public int findSubjectCount();//查询试题总量
public Subject findSubjectByID(int subjectID);//根据试题ID查找试题
public void updateSubject(Subject subject);//更新方法,用来更新试题
public void deleteSubject(int subjectID);//根据试题ID删除试题
public List<Subject> likeQueryByTitle(String subjectTitle,Page page);//根据试题标题模糊查询试题
public int findLinkQueryCount(String subjectTitle);//查询模糊记录数
public List<Subject> randomFindSubject(int number);//随时取出记录
}

4.

 package com.sanqing.dao;

 import java.util.List;

 import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction; import com.sanqing.hibernate.HibernateSessionFactory;
import com.sanqing.po.Student;
import com.sanqing.po.Subject;
import com.sanqing.util.Page; public class SubjectDAOImpl implements SubjectDAO{
public void addSubject(Subject subject){
Session session = HibernateSessionFactory.getSession();//获得Session对象
Transaction transaction = null;//声明一个事务对象
try{
transaction = session.beginTransaction();//开启事务
session.save(subject);//保存试题信息
transaction.commit();//提交事务
}catch(Exception ex) {
ex.printStackTrace();
transaction.rollback();//事务回滚
}
HibernateSessionFactory.closeSession();//关闭Session对象
} public Subject findSubjectByTitle(String subjectTitle) {
Session session = HibernateSessionFactory.getSession();//获得Session对象
Query query = session.createQuery("from Subject as sub where sub.subjectTitle = ?");
query.setString(0, subjectTitle);
List list = query.list(); //查询结果保存到list中
HibernateSessionFactory.closeSession(); //关闭Session对象
if(list.size() == 0) {
return null; //返回null
}else {
return (Subject) list.get(0); //返回第一个试题
}
} public List<Subject> findSubjectByPage(Page page) {
Session session = HibernateSessionFactory.getSession();//获得Session对象
Query query = session.createQuery("from Subject");
query.setMaxResults(page.getEveryPage());//设置查询记录数
query.setFirstResult(page.getBeginIndex());//设置查询记录起始位置
List list = query.list(); //查询结果保存到list中
HibernateSessionFactory.closeSession();//关闭Session对象
return list;
} public int findSubjectCount() {
Session session = HibernateSessionFactory.getSession();//获得Session对象
Query query = session.createQuery("from Subject");
List list = query.list(); //查询结果保存到list中
int count = list.size();
HibernateSessionFactory.closeSession();//关闭Session对象
return count;
} public Subject findSubjectByID(int subjectID) {
Session session = HibernateSessionFactory.getSession();//获得Session对象
Subject subject = (Subject) session.get(Subject.class, subjectID);
HibernateSessionFactory.closeSession(); //关闭Session对象
return subject;
} public void updateSubject(Subject subject) {
Session session = HibernateSessionFactory.getSession();//获得Session对象
Transaction transaction = null;//声明一个事务对象
try{
transaction = session.beginTransaction();//开启事务
session.update(subject);//更新试题信息
transaction.commit();//提交事务
}catch(Exception ex) {
ex.printStackTrace();
transaction.rollback();//事务回滚
}
HibernateSessionFactory.closeSession();//关闭Session对象
} public void deleteSubject(int subjectID) {
Session session = HibernateSessionFactory.getSession();//获得Session对象
Subject subject = (Subject) session.get(Subject.class, subjectID);
Transaction transaction = null;//声明一个事务对象
try{
transaction = session.beginTransaction();//开启事务
session.delete(subject);
transaction.commit();//提交事务
}catch(Exception ex) {
ex.printStackTrace();
transaction.rollback();//事务回滚
}
HibernateSessionFactory.closeSession();//关闭Session对象
} public List<Subject> likeQueryByTitle(String subjectTitle,Page page) {
Session session = HibernateSessionFactory.getSession();//获得Session对象
Query query = session.createQuery("from Subject as sub where sub.subjectTitle like :title ");
query.setString("title","%"+subjectTitle+"%");
query.setMaxResults(page.getEveryPage());//设置查询记录数
query.setFirstResult(page.getBeginIndex());//设置查询记录起始位置
List list = query.list(); //查询结果保存到list中
HibernateSessionFactory.closeSession();//关闭Session对象
return list;
} public int findLinkQueryCount(String subjectTitle) {
Session session = HibernateSessionFactory.getSession();//获得Session对象
Query query = session.createQuery("from Subject as sub where sub.subjectTitle like :title ");
query.setString("title","%"+subjectTitle+"%");
List list = query.list(); //查询结果保存到list中
int count = list.size();
HibernateSessionFactory.closeSession();//关闭Session对象
return count;
} public List<Subject> randomFindSubject(int number) {
Session session = HibernateSessionFactory.getSession();//获得Session对象
Query query = session.createQuery("from Subject as sub order by rand()");
query.setMaxResults(number);//设置查询记录数
List list = query.list(); //查询结果保存到list中
HibernateSessionFactory.closeSession();//关闭Session对象
return list;
}
}

5.

 package com.sanqing.dao;

 import com.sanqing.po.Teacher;

 public interface TeacherDAO {
public Teacher findByTeacherID(String teacherID);//查询方法,根据教师ID查询
}

6.

 package com.sanqing.dao;

 import org.hibernate.Session;

 import com.sanqing.hibernate.HibernateSessionFactory;
import com.sanqing.po.Teacher; public class TeacherDAOImpl implements TeacherDAO{
public Teacher findByTeacherID(String teacherID) {
Session session = HibernateSessionFactory.getSession();//获得Session对象
Teacher teacher = (Teacher) session.get(Teacher.class, teacherID);
HibernateSessionFactory.closeSession();//关闭Session对象
return teacher;
}
}

JavaWeb项目开发案例精粹-第3章在线考试系统-003Dao层的更多相关文章

  1. JavaWeb项目开发案例精粹-第3章在线考试系统-007View层

    0.login.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...

  2. JavaWeb项目开发案例精粹-第3章在线考试系统-005action层

    1. <?xml version="1.0" encoding="UTF-8" ?><!-- XML声明 --> <!DOCTYP ...

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

    1. package com.sanqing.service; import java.util.List; import com.sanqing.po.Student; public interfa ...

  4. JavaWeb项目开发案例精粹-第3章在线考试系统-006实体层

    1. package com.sanqing.po; /* * 学生表,保存学生编号,系统密码 */ public class Student { private String studentID; ...

  5. JavaWeb项目开发案例精粹-第3章在线考试系统-002配置文件及辅助类

    1. <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5&qu ...

  6. JavaWeb项目开发案例精粹-第3章在线考试系统-001设计

    1. 2. 3. 4. # MySQL-Front 5.0 (Build 1.0) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET SQL ...

  7. JavaWeb项目开发案例精粹-第6章报价管理系统-05Action层

    0. <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC &quo ...

  8. JavaWeb项目开发案例精粹-第6章报价管理系统-002辅助类及配置文件

    1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...

  9. JavaWeb项目开发案例精粹-第2章投票系统-001设计

    1.项目结构 2.数据库设计 # MySQL-Front 5.0 (Build 1.0) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET ...

随机推荐

  1. Swift初步介绍

    Swift是本届WWDC大会苹果推出的一门新开发语言,开发者网站上已经放出了这门新语言的介绍.教程和手册,如果手里有一台iOS设备的话,通过苹果的iBooks应用,从它的官方书店里搜索Swift,可以 ...

  2. trade 主要前端组件

    jQuery Custombox http://www.jqueryfuns.com/resource/view/27

  3. Oracle 表的连接方式(2)-----HASH JOIN的基本机制2

    Hash算法原理 对于什么是Hash算法原理?这个问题有点难度,不是很好说清楚,来做一个比喻吧:我们有很多的小猪,每个的体重都不一样,假设体重分布比较平均(我们考虑到公斤级别),我们按照体重来分,划分 ...

  4. 第29章 项目10:DIY街机游戏

    1.问题 "Self-Defense Against Fresh Fruit":军士长指挥自己的士兵使用自我防御战术对抗以石榴.芒果.青梅和香蕉等新鲜水果入侵者.防御战术包括使用枪 ...

  5. MySQL监控工具-orzdba

    源代码地址:http://code.taobao.org/p/orzdba/src/trunk/     [root@hank-yoon servers]# chmod +x orzdba 在代码的1 ...

  6. MYSQL创建多张表,相同表结构,不同表名

    #!/bin/bashfor i in {0..63}domysql -u$1 -p$2 -h127.0.0.1 <<EOFuse yoon;create table ivc_pre_de ...

  7. MyEclipse反编译Class文件

    对于需要查看Java Class文件源码的筒子们来说,必须在项目中导入Java源码才能查看Class文件的具体实现,这不仅十分的麻烦,因为有时我们并不可以获得Class文件对应的Java源码.今天就给 ...

  8. 【nodejs】 npm 注意事项

    官网:https://www.npmjs.com/ 1.安装时要切换到nodejs根目录,  否则就会安装到安装时所在的目录 2.要有管理员权限(win),如需指定版本,如npm install ex ...

  9. iOS下日期的处理(世界标准时转本地时间)

    NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates         NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是 ...

  10. APACHE 403 FORBIDDEN错误的解决办法之一

    打开 apache的配置文件httpd.conf,找到这段代码: Options FollowSymLinksAllowOverride NoneOrder deny,allowDeny from a ...