Hibernate 改写管理系统的业务功能
public class StudentDaoImpl implements StudentDao { @Override /** * 查询所有学生 * * @throws SQLException */ public List<Student> findAll() throws SQLException { Session session = HibernateUtils.oppenSession(); Transaction transaction = session.beginTransaction(); // HQL查询,面向对象的查询语言,from Student Query query = session.createQuery("from Student"); List<Student> list = query.list(); transaction.commit(); session.close(); return list; } @Override /** * 需要添加到数据库的学生 */ public void insert(Student student) throws SQLException { Session session = HibernateUtils.oppenSession(); Transaction transaction = session.beginTransaction(); // HQL查询,面向对象的查询语言 session.save(student); transaction.commit(); session.close(); } @Override /** * 根据id删除学生 */ public void delete(int sid) throws SQLException { Session session = HibernateUtils.oppenSession(); Transaction transaction = session.beginTransaction(); // HQL查询,面向对象的查询语言 Student student = session.get(Student.class, sid); session.delete(student); transaction.commit(); session.close(); } /** * 根据id查询单个学生 * * @throws SQLException */ @Override public Student findStudentById(int sid) throws SQLException { Session session = HibernateUtils.oppenSession(); Transaction transaction = session.beginTransaction(); // HQL查询,面向对象的查询语言 Student student = session.get(Student.class, sid); transaction.commit(); session.close(); return student; } @Override /** * 更新学生到数据库 */ public void Update(Student stu, int sid) throws SQLException { Session session = HibernateUtils.oppenSession(); Transaction transaction = session.beginTransaction(); stu.setSid(sid); session.saveOrUpdate(stu); transaction.commit(); session.close(); } @Override /** * 模糊查询的DAO层实现 */ public List<Student> SearchStident(String sname, String sgender) throws SQLException { DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Student.class); Object[] obj = { null, "男", "女" }; detachedCriteria.add(Restrictions.like("sname", "%" + sname + "%")); detachedCriteria.add(Restrictions.in("gender", obj)); Session session = HibernateUtils.oppenSession(); Transaction transaction = session.beginTransaction(); Criteria criteria = detachedCriteria.getExecutableCriteria(session); List<Student> list = criteria.list(); transaction.commit(); session.close(); return list; } @Override /** * 分页查询,查询当前页的学生数据并封装成集合 */ public List<Student> findStudentByPage(int currentPage) throws SQLException { Session session = HibernateUtils.oppenSession(); Transaction transaction = session.beginTransaction(); Criteria criteria = session.createCriteria(Student.class); criteria.setFirstResult(PAGE_SIZE * (currentPage - 1));//起始位置 criteria.setMaxResults(PAGE_SIZE);//每页多少条记录 List<Student> list = criteria.list(); return list; /*QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); // 第一个?返回记录数量,第二个?偏移量 // 第n页 : 5,5*n-1 return runner.query("select * from students limit ? offset ?", new BeanListHandler<Student>(Student.class), PAGE_SIZE, PAGE_SIZE * (currentPage - 1));*/ } @Override /** * 查询学生记录数 */ public int findCount() throws SQLException { Session session = HibernateUtils.oppenSession(); Transaction transaction = session.beginTransaction(); Criteria criteria = session.createCriteria(Student.class); criteria.setProjection(Projections.rowCount()); Long num = (Long) criteria.uniqueResult(); return num.intValue(); } }