hibernate分页查询

╰+哭是因爲堅強的太久メ 2022-07-13 08:52 359阅读 0赞

一、准备工作

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

Center

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

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

  1. public class TestPage {
  2. public static void main(String[] args) {
  3. // t1();//通过分页类分页查询
  4. t2();//直接在hibernate中分页查询
  5. }
  6. }

二、开始测试

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

  1. package com.svse;
  2. import java.util.List;
  3. /**
  4. * 新建一个泛型类Pager<T>,这是分页的公共类 <br/>
  5. *
  6. * @author Administrator
  7. * 分页 <br/>
  8. * firstIndex 首记录索引 = (pageNo - 1) * pageSize <br/>
  9. * pageNo 当前页号 <br/>
  10. * pageSize 页面大小 <br/>
  11. * totalRecordCount 总记录数 = select count(*) from 表名 <br/>
  12. * totalPageCount 总页数 = <br/>
  13. * 算法1: if( totalRecourdCount % pageSize == 0 ){ <br/>
  14. * totalPageCount = totalRecourdCount / pageSize; <br/>
  15. * }else{ <br/>
  16. * totalPageCount = totalRecourdCount / pageSize + 1; <br/>
  17. * } <br/>
  18. *
  19. * 算法2: totalPageCount = (totalRecourdCount - 1) / pageSize + 1; <br/>
  20. * @param <T>
  21. */
  22. public class Pager<T> {
  23. private int firstIndex;// 首记录索引
  24. private int pageNo;// 当前页号
  25. private int pageSize;// 页面大小
  26. private int totalRecordCount;// 总记录数
  27. private int totalPageCount;// 总页数
  28. private List<T> list;// list容器,接收数据
  29. public List<T> getList() {
  30. return list;
  31. }
  32. public void setList(List<T> list) {
  33. this.list = list;
  34. }
  35. /**
  36. * firstIndex只要get方法,不要set方法
  37. *
  38. * @return
  39. */
  40. public int getFirstIndex() {
  41. // 首记录索引 = (当前页号 - 1)*页面大小
  42. return firstIndex = (this.getPageNo() - 1) * this.getPageSize();
  43. }
  44. public int getPageNo() {
  45. return pageNo;
  46. }
  47. public void setPageNo(int pageNo) {
  48. this.pageNo = pageNo;
  49. }
  50. public int getPageSize() {
  51. return pageSize;
  52. }
  53. public void setPageSize(int pageSize) {
  54. this.pageSize = pageSize;
  55. }
  56. public int getTotalRecordCount() {
  57. return totalRecordCount;
  58. }
  59. public void setTotalRecordCount(int totalRecordCount) {
  60. this.totalRecordCount = totalRecordCount;
  61. }
  62. /**
  63. * totalPageCount只要get方法,不要set方法
  64. *
  65. * @return
  66. */
  67. public int getTotalPageCount() {
  68. // 总页数 = (总记录数 - 1)/页面大小 + 1
  69. return totalPageCount = (this.getTotalRecordCount() - 1)
  70. / this.getPageSize() + 1;
  71. }
  72. }

2.再到测试类TestPage的主方法中编写测试方法,并调用这个类,来实现分页查询

  1. /**
  2. * 通过分页类分页查询
  3. */
  4. private static void t1() {
  5. Configuration config = new Configuration().configure();
  6. SessionFactory sessionFactory = config.buildSessionFactory();
  7. Session session = sessionFactory.openSession();
  8. Transaction transaction = session.beginTransaction();
  9. //查询emp表的所有内容
  10. Query query = session.createQuery("FROM Emp");
  11. //将Emp填充到Pager<T>泛型类中
  12. Pager<Emp> pager = new Pager<Emp>();
  13. pager.setPageNo(2);//当前页号
  14. pager.setPageSize(5);//页面大小
  15. query.setFirstResult(pager.getFirstIndex());//获取首记录索引
  16. query.setMaxResults(pager.getPageSize());//获取页面大小
  17. List<Emp> list = query.list();
  18. for(Emp e : list){
  19. /*
  20. * e.getDept()获得的是一个地址
  21. * e.getDept().getDeptno()获得的才是真正的部门编号
  22. * */
  23. System.out.println(e.getEmpno()+"\t\t"+e.getEname()+"\t\t"+e.getJob()+"\t\t"
  24. +e.getMgr()+"\t\t"+e.getHiredate()+"\t\t"
  25. +e.getSal()+"\t\t"+e.getComm()+"\t\t"+e.getDept().getDeptno());
  26. }
  27. transaction.commit();
  28. session.close();
  29. sessionFactory.close();
  30. }

首先是e.getDept(),获取部门

Center 1

Center 2

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

Center 3

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

  1. /**
  2. * 直接在hibernate中分页查询
  3. */
  4. private static void t2() {
  5. Configuration config = new Configuration().configure();
  6. SessionFactory sessionFactory = config.buildSessionFactory();
  7. Session session = sessionFactory.openSession();
  8. Transaction transaction = session.beginTransaction();
  9. Query query = session.createQuery("FROM Emp");
  10. //查询emp表中的总记录数countHQL
  11. String countHQL = "SELECT count(*) FROM Emp";
  12. //计算总记录数count
  13. int count = ((Long) session.createQuery(countHQL).uniqueResult()).intValue();
  14. //设置页面大小pageSize
  15. int pageSize = 5;
  16. //计算总页数totalPageCount
  17. int totalPageCount = (count%pageSize == 0)?(count/pageSize):(count/pageSize + 1);
  18. //设置当前页码pageNo
  19. int pageNo = 1;
  20. query.setFirstResult((pageNo - 1)*pageSize);//获取首记录索引
  21. query.setMaxResults(pageSize);//获取页面大小
  22. List<Emp> list = query.list();
  23. for(Emp e : list){
  24. /*
  25. * e.getDept()获得的是一个地址
  26. * e.getDept().getDeptno()获得的才是真正的部门编号
  27. * */
  28. System.out.println(e.getEmpno()+"\t\t"+e.getEname()+"\t\t"+e.getJob()+"\t\t"
  29. +e.getMgr()+"\t\t"+e.getHiredate()+"\t\t"
  30. +e.getSal()+"\t\t"+e.getComm()+"\t\t"+e.getDept().getDeptno());
  31. }
  32. transaction.commit();
  33. session.close();
  34. sessionFactory.close();
  35. }

运行结果为:

Center 4

关键的地方在:

  1. query.setFirstResult((pageNo - 1)*pageSize);//获取首记录索引
  2. query.setMaxResults(pageSize);//获取页面大小

要记住这两个set方法。

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

发表评论

表情:
评论列表 (有 0 条评论,359人围观)

还没有评论,来说两句吧...

相关阅读