hibernate分页查询

来源:互联网 发布:怎么创建app软件 编辑:程序博客网 时间:2024/06/06 12:23

一、准备工作

1.打开oracle数据库,登录scott账户,查询emp表的所有记录、总记录数

2.打开MyEclipse,创建一个Java项目:hibernate_page,创建包,添加oracle数据库连接,添加hibernate配置文件,添加scott账户emp,dept表的hibernate映射文件及映射类。

3.创建TestPage.java测试类,测试hibernate分页查询。

public class TestPage {public static void main(String[] args) {//t1();//通过分页类分页查询t2();//直接在hibernate中分页查询}
}
二、开始测试

(1)1.通过创建一个泛型类Pager<T>,这是分页的公共类

package com.svse;import java.util.List;/** * 新建一个泛型类Pager<T>,这是分页的公共类<br/> *  * @author Administrator  * 分页<br/> *  firstIndex 首记录索引 = (pageNo - 1) * pageSize <br/> *  pageNo 当前页号 <br/> *  pageSize 页面大小<br/> *  totalRecordCount 总记录数 = select count(*) from 表名<br/> *  totalPageCount 总页数 = <br/> *         算法1: if( totalRecourdCount % pageSize == 0 ){ <br/> *         totalPageCount = totalRecourdCount / pageSize; <br/> *           }else{ <br/> *         totalPageCount = totalRecourdCount / pageSize + 1;  <br/> *           }<br/> *  *         算法2: totalPageCount = (totalRecourdCount - 1) / pageSize + 1;<br/> * @param <T> */public class Pager<T> {private int firstIndex;// 首记录索引private int pageNo;// 当前页号private int pageSize;// 页面大小private int totalRecordCount;// 总记录数private int totalPageCount;// 总页数private List<T> list;// list容器,接收数据public List<T> getList() {return list;}public void setList(List<T> list) {this.list = list;}/** * firstIndex只要get方法,不要set方法 *  * @return */public int getFirstIndex() {// 首记录索引 = (当前页号 - 1)*页面大小return firstIndex = (this.getPageNo() - 1) * this.getPageSize();}public int getPageNo() {return pageNo;}public void setPageNo(int pageNo) {this.pageNo = pageNo;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public int getTotalRecordCount() {return totalRecordCount;}public void setTotalRecordCount(int totalRecordCount) {this.totalRecordCount = totalRecordCount;}/** * totalPageCount只要get方法,不要set方法 *  * @return */public int getTotalPageCount() {// 总页数 = (总记录数 - 1)/页面大小 + 1return totalPageCount = (this.getTotalRecordCount() - 1)/ this.getPageSize() + 1;}}
2.再到测试类TestPage的主方法中编写测试方法,并调用这个类,来实现分页查询

/** * 通过分页类分页查询 */private static void t1() {Configuration config = new Configuration().configure();SessionFactory sessionFactory = config.buildSessionFactory();Session session = sessionFactory.openSession();Transaction transaction = session.beginTransaction();//查询emp表的所有内容Query query = session.createQuery("FROM Emp");//将Emp填充到Pager<T>泛型类中Pager<Emp> pager = new Pager<Emp>();pager.setPageNo(2);//当前页号pager.setPageSize(5);//页面大小query.setFirstResult(pager.getFirstIndex());//获取首记录索引query.setMaxResults(pager.getPageSize());//获取页面大小List<Emp> list = query.list();for(Emp e : list){/* * e.getDept()获得的是一个地址 * e.getDept().getDeptno()获得的才是真正的部门编号 * */System.out.println(e.getEmpno()+"\t\t"+e.getEname()+"\t\t"+e.getJob()+"\t\t"+e.getMgr()+"\t\t"+e.getHiredate()+"\t\t"+e.getSal()+"\t\t"+e.getComm()+"\t\t"+e.getDept().getDeptno());}transaction.commit();session.close();sessionFactory.close();}
首先是e.getDept(),获取部门


再将e.getDept()改为e.getDept().getDeptno().


(2)直接在hibernate中分页查询

/** * 直接在hibernate中分页查询 */private static void t2() {Configuration config = new Configuration().configure();SessionFactory sessionFactory = config.buildSessionFactory();Session session = sessionFactory.openSession();Transaction transaction = session.beginTransaction();Query query = session.createQuery("FROM Emp");//查询emp表中的总记录数countHQLString countHQL = "SELECT count(*) FROM Emp";//计算总记录数countint count = ((Long) session.createQuery(countHQL).uniqueResult()).intValue();//设置页面大小pageSizeint pageSize = 5;//计算总页数totalPageCountint totalPageCount = (count%pageSize == 0)?(count/pageSize):(count/pageSize + 1);//设置当前页码pageNoint pageNo = 1;query.setFirstResult((pageNo - 1)*pageSize);//获取首记录索引query.setMaxResults(pageSize);//获取页面大小List<Emp> list = query.list();for(Emp e : list){/* * e.getDept()获得的是一个地址 * e.getDept().getDeptno()获得的才是真正的部门编号 * */System.out.println(e.getEmpno()+"\t\t"+e.getEname()+"\t\t"+e.getJob()+"\t\t"+e.getMgr()+"\t\t"+e.getHiredate()+"\t\t"+e.getSal()+"\t\t"+e.getComm()+"\t\t"+e.getDept().getDeptno());}transaction.commit();session.close();sessionFactory.close();}

运行结果为:


关键的地方在:

query.setFirstResult((pageNo - 1)*pageSize);//获取首记录索引query.setMaxResults(pageSize);//获取页面大小
要记住这两个set方法。

好了,在hibernate中分页查询就完成了。^_^





0 0