1,First of all,  we should have a wrapper class for page,this class can calculate the startRow by the number of result,pageSize,and currentPage.

public class Pager extends BaseBO {
private static final int DEFAULT_PAGE_SIZE = 10;
private int totalRows;
private int pageSize = 10;
private int currentPage;
private int totalPages;
private int startRow;
public Pager(int _totalRows, int _pageSize, int _currentPage) {
init(_totalRows, _pageSize, _currentPage);
}
private void init(int _totalRows, int _pageSize, int _currentPage) {
totalRows = _totalRows;
pageSize = _pageSize;
currentPage = _currentPage; if (totalRows < 0) {
totalRows = 0;
} if (pageSize < 1) {
pageSize = DEFAULT_PAGE_SIZE;
}
totalPages = totalRows / pageSize;
int mod = totalRows % pageSize;
if (mod > 0) {
totalPages++;
}
if (currentPage > totalPages) {
currentPage = totalPages;
}
if (currentPage < 1) {
currentPage = 1;
}
startRow = (currentPage - 1) * pageSize;
}
}

then ,we should have a wrapper class for pageList:

public class PageList extends BaseBO {

    /** 分页信息 */
private Pager pager; /** 页记录列表 */
private List list;
................//omit the method of get and set
}

2,In  the DAO,we can see :

public class YfsjIssueDAOImpl extends BaseHibernateDaoImpl{
public PageList findLogicB1(YfsjIssueModel condition){
..............//service code omitted
return findPageListWithHql(countHql, qryHql,pageSize,currentPage);

from this code,we find that all the DAO  extend  from the Class of BaseHibernateDaoImpl .And the Class of BaseHibernateDaoImpl declare a method called findPageListWithHql  to control paging.
3,then let's learn this paging method declared in BaseHibernateDaoImpl :

abstract public class BaseHibernateDaoImpl extends HibernateDaoSupport implements BaseHibernateDao { /**
* find the paging result
*
* @param countHql
* find the number of result by HQL
* @param qryHql
* find the result by HQL
* @param pageSize
* the number of result by each page
* @param currentPage
* the page number
* @return
*/
protected PageList findPageListWithHql(String countHql, String qryHql, int pageSize, int currentPage) {
return findPageListWithHql(countHql, qryHql, null, pageSize, currentPage);
}
   protected PageList findPageListWithHql(String countHql, String qryHql, List paramList, int pageSize, int currentPage) {
PageList list = null;
   int totalRows = getCount(countHql, paramList);  //get the number of result if (totalRows < 1) {
return list;
}
  
list = new PageList();
Pager pager = new Pager(totalRows, pageSize, currentPage); //create a new instance of the page model
list.setPager(pager);
list.setList(this.findPageList(qryHql, paramList, pager.getStartRow(), pager.getPageSize()));
return list;
}

then,let's learn the method called getCount()  to get the number of result,  this method is very easy.

 protected int getCount(String hql, List paramList) {
try {
Query query = this.getSession().createQuery(hql); if (paramList != null && paramList.size() > 0) {
for (int i = 0; i < paramList.size(); i++) {
query.setParameter(i, paramList.get(i));
}
} List list = query.list();
return ((Integer) list.get(0)).intValue();
} catch (HibernateException ex) {
throw new RuntimeException(ex);
}
}

then ,let's see the last method  called findPageList that to  find the result :

 protected List findPageList(String hql, List paramList, int startRow, int pageSize) {
try {
Query query = this.getSession().createQuery(hql); if (paramList != null && paramList.size() > 0) {
for (int i = 0; i < paramList.size(); i++) {
query.setParameter(i, paramList.get(i));
}
} query.setFirstResult(startRow);
query.setMaxResults(pageSize);
return query.list();
} catch (HibernateException ex) {
throw new RuntimeException(ex);
}
}

Java项目中基于Hibernate分页总结的更多相关文章

  1. java环境中基于jvm的两大语言:scala,groovy

    一.java环境中基于jvm的两大语言:scala,groovy 可以在java项目里混编这两种语言: scala:静态语言,多范式语言,糅合了面向对象.面向过程:可以与java和net互操作:融汇了 ...

  2. 在Java项目中整合Scala

    Scala是一个运行在Java JVM上的面向对象的语言.它支持函数编程,在语法上比Java更加灵活,同时通过Akka库,Scala支持强大的基于Actor的多线程编程.具有这些优势,使得我最近很想在 ...

  3. XML在JAVA项目中的作用

    java项目中,xml文件一般都是用来存储一些配置信息 一般的编程, 多数用来存储配置信息 . 拿JDBC来说,可以把数据库连接字符串写到xml,如果要修改数据源,只需要改xml就可以了,没必要再去重 ...

  4. ckeditor编辑器在java项目中配置

    一.基本使用: 1.所需文件架包 A. Ckeditor基本文件包,比如:ckeditor_3.6.2.zip 下载地址:http://ckeditor.com/download 2.配置使用 A.将 ...

  5. Ant在Java项目中的使用(一眼就看会)

    参考:http://www.cnblogs.com/zhengqiang/p/5557155.html Ant是跨平台的构建工具,它可以实现项目的自动构建和部署等功能.在本文中,主要让读者熟悉怎样将A ...

  6. JAVA 项目中使用 H2 数据库

    为什么要使用H2数据库 H2数据库是可以嵌入到JAVA项目中的,因为只需要导入一个jar包即可,所以非常的方便. 项目中导入H2 将H2的jar包放到classpath里即可,我是用的maven,ma ...

  7. UCenter在JAVA项目中实现的单点登录应用实例

    Comsenz(康盛)的UCenter当前在国内的单点登录领域占据绝对份额,其完整的产品线令UCenter成为了账号集成方面事实上的标准. 基于UCenter,可以将Comsenz旗下的Discuz! ...

  8. 实战派 | Java项目中玩转Redis6.0客户端缓存!

    原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 哈喽大家好啊,我是Hydra. 在前面的文章中,我们介绍了Redis6.0中的新特性客户端缓存client-side caching,通过tel ...

  9. eclipse java项目中明明引入了jar包 为什么项目启动的时候不能找到jar包 项目中已经 引入了 com.branchitech.app 包 ,但时tomcat启动的时候还是报错? java.lang.ClassNotFoundException: com.branchitech.app.startup.AppStartupContextListener java.lang.ClassN

    eclipse java项目中明明引入了jar包 为什么项目启动的时候不能找到jar包 项目中已经 引入了 com.branchitech.app 包 ,但时tomcat启动的时候还是报错?java. ...

随机推荐

  1. Morgan Stanley telephone interview

    Today is Monday, April 28. I get a telephone call from Morgan Stanley in Shanghai. My examiner is a ...

  2. 深入理解Python中的生成器

    生成器(generator)概念 生成器不会把结果保存在一个系列中,而是保存生成器的状态,在每次进行迭代时返回一个值,直到遇到StopIteration异常结束. 生成器语法 生成器表达式: 通列表解 ...

  3. 两个for循环例子

    var i,j; var a=0; // for(i=0,j=0;i<5,j<7;i++,j++){ // a=i+j; // } // alert(a) //12 for(i=0,j=0 ...

  4. Boolean 布尔类型详解

    这是最简单的类型.boolean 表达了真值,可以为 TRUE 或 FALSE.两个都不区分大小写. 要明确地将一个值转换成 boolean,用 (bool)或者 (boolean) 来强制转换.但是 ...

  5. 此文件时入口文件index.php

    此文件时入口文件index.php <?php //定义一下ThinkPHP框架存放的路径 define('THINK_PATH','./ThinkPHP/'); //定义当前的项目的名称,此处 ...

  6. html meta标签用法详细介绍

    meta是html语言head区的一个辅助性标签. 在页面中都有类似这样的html代码: <head> <meta http-equiv="content-Type&quo ...

  7. python 数据类型(元组(不可变列表),字符串

    元组(不可变列表) 创建元组: ages = (11, 22, 33, 44, 55) 或 ages = tuple((11, 22, 33, 44, 55)) 一般情况下是不需要被人该的数值才使用元 ...

  8. 用19种编程语言写Hello World

    用19种编程语言写Hello World 转载自:http://www.admin10000.com/document/394.html Hello World 程序是每一种编程语言最基本的程序,通常 ...

  9. bzoj 2852: 强大的区间 辗转相除

    2852: 强大的区间 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 45  Solved: 12[Submit][Status][Discuss] D ...

  10. 【技术帖】解决 Hudson jenkins 连接等待中 - Waiting for next av

    今天构建项目发现如下问题: jenkins 连接等待中 - Waiting for next available executor 左下角那块一直不运行构建,一直在连接等待. 于是,进入一级页面, 右 ...