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. Mac苹果电脑加密视频播放器使用教程

    1.   下载文件 https://pan.baidu.com/s/1slhFYuL 2.    操作流程 温馨提示 播放时,请务必保证播放设备联网(原因:用户名权限验证需要网络,播放后10秒即可关闭 ...

  2. 项目规范和建立-从frozenui学习

    1.分支branches开发新功能,主干trunk是稳定发布的.因为frozenui下载完,看到branches比trunk多了一个版本 2.版本号定义,主版本.次版本.修订号 大版本号: 主版本号: ...

  3. PHP异常处理

    一.异常处理——可以有效地控制多条出现错误或异常的代码 基本语法如下: try{ //可能出现异常的代码 } catch(Exception $e){ //对异常处理 //1.自己处理 //2.不作处 ...

  4. ODBC方式连接Informix数据库

    公司某个报表系统使用Informix数据库,在谋划使用Perl语言写数据采集程序后,花费了很多时间建立Perl访问Informix连接.恰巧Windows下ActivePerl的CPAN中又没有DBD ...

  5. (转载)Delphi StringGrid常用属性和常用操作

    Delphi StringGrid常用属性和常用操作 StringGrid组件用于建立显示字符串的网格,与电子表格相似.它可使表格中的字符串和相关对象操作简单化.StringGrid组件提供了许多可控 ...

  6. Kivy中文显示

    Win7系统 下载 DroidSansFallback.ttf字体(android设备上自带了) 源代码第一行增加#-*- coding:utf-8 -*- 创建widget值定font_name s ...

  7. Python学习笔记——几种数据类型

    1. 列表list: Python内置的一种数据类型是列表:list,用中括号[]表示.list是一种有序的集合,可以随时添加和删除其中的元素,而且元素的类型不必相同.list可以通过下标来访问,范围 ...

  8. sql join 用法

    SQL JOIN 的用法   关于sql语句中的连接(join)关键字,是较为常用而又不太容易理解的关键字,下面这个例子给出了一个简单的解释 --建表table1,table2:create tabl ...

  9. windows2003网络负载平衡设置

    问题 随着计算机技术的不断发展,单台计算机的性能和可靠性越来越高.但现实中还是有许多应用是单台计算机难以达到,例如: 1.银行存储用户数据的数据库服务器必须保证24小时不间断的运转,并在发生严重硬件故 ...

  10. phpwind9.0 顶部和底部版权信息永久性修改

    过了pw头部和底部版权修改方法,但是每次升级程序后版权又变成了默认的了,还得重新修改,其实有个方法可以永久性修改,底部和顶部随着主题走. pw9全局主题位于/themes/site/目录下,  前面文 ...