JavaWeb项目开发案例精粹-第3章在线考试系统-003Dao层
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层的更多相关文章
- JavaWeb项目开发案例精粹-第3章在线考试系统-007View层
0.login.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...
- JavaWeb项目开发案例精粹-第3章在线考试系统-005action层
1. <?xml version="1.0" encoding="UTF-8" ?><!-- XML声明 --> <!DOCTYP ...
- JavaWeb项目开发案例精粹-第3章在线考试系统-004Service层
1. package com.sanqing.service; import java.util.List; import com.sanqing.po.Student; public interfa ...
- JavaWeb项目开发案例精粹-第3章在线考试系统-006实体层
1. package com.sanqing.po; /* * 学生表,保存学生编号,系统密码 */ public class Student { private String studentID; ...
- JavaWeb项目开发案例精粹-第3章在线考试系统-002配置文件及辅助类
1. <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5&qu ...
- JavaWeb项目开发案例精粹-第3章在线考试系统-001设计
1. 2. 3. 4. # MySQL-Front 5.0 (Build 1.0) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET SQL ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-05Action层
0. <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC &quo ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-002辅助类及配置文件
1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...
- JavaWeb项目开发案例精粹-第2章投票系统-001设计
1.项目结构 2.数据库设计 # MySQL-Front 5.0 (Build 1.0) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET ...
随机推荐
- js学习笔记一-语法结构
js是区分大小写的,关键字.变量.函数名和所有的标识符都必须采取统一一致的大小写形式. js定义了unicode转义序列,以\u开头,其后跟随四个十六进制数,可以在字符串直接量.正则表达式直接量和标识 ...
- WPF的Application类
本节主要介绍一下Application类的部分功能,我们首先来看一下如何使用Application类来加载一个窗口: 我们首先创建一个控制台程序,并引入相关的dll,然后修改Main()方法. [ST ...
- OSGI容器与插件
插件必须符合osgi规范才能插到osgi容器中,osgi容器查看插件jar中MANIFEST.MF中osgi容器. 所谓插件----就是打包好的jar文件, 内部都封装好了一些功能
- HTTP 错误405.0 - Method Not Allowed
在使用IIS发布网页的时候,出现下图这个问题 那么出现这个问题的原因可能有很多种,但是我目前是这么解决的,下面说一下我个人解决这个问题的方案,仅供参考. 首先,以管理员的身份启动cmd 如果电脑是64 ...
- Object.keys()
Object.keys(obj),返回一个数组,数组里是该obj可被枚举的所有属性名.请看示例: 示例一: function Pasta(grain, width, shape) { this.gra ...
- 关于WP8 微信分享的补充说明
1.根据微信官方Demo完成相应功能. 2.在分享完后,从微信回来,需要进行 快速恢复. 3.在快速恢复中加入 RootFrame.Navigating += HandlerFotResetNavig ...
- JavaScript Tutorial
JavaScript Tutorial http://javascript.info/root Object.create rabit.hasOwnProperty('eats') Object.ge ...
- SCRUM报告(一)
我们“来用”团队确定的PM是邓锐.这是我们第一篇SCRUM报告,报告的内容就是我们的Sprint会议.之前冲刺计划会议的内容已发博客,这里简单阐述一下. 一.会议过程大致如下: 1.总结目前的工作进展 ...
- tomcat服务
参考资料:http://www.chysoft.net/showinfo.asp?id=84 (1) Tomcat服务的安装 说明: 默认情况下,tomcat的服务是未安装的,具体tomcat服务的安 ...
- soft particles
http://developer.download.nvidia.com/whitepapers/2007/SDK10/SoftParticles_hi.pdf nvdia ------------- ...