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. python 自动化之路 day 08_2 网络编程

    本节内容 Socket介绍 Socket参数介绍 基本Socket实例 Socket实现多连接处理 通过Socket实现简单SSH 通过Socket实现文件传送 作业:开发一个支持多用户在线的FTP程 ...

  2. docker 中搭建tomcat

    关于docker中tomcat的搭建,没有什么多说的,直接下载官方镜像 docker pull tomcat  (可能会下载到好几个镜像,就是tag不同,有知道为什么的可以告诉我) 然后我看的  ht ...

  3. 【DP_树形DP专题】题单总结

    转载自 http://blog.csdn.net/woshi250hua/article/details/7644959#t2 题单:http://vjudge.net/contest/123963# ...

  4. php学习,一个简单的Calendar(2) 一个简单的活动页面

    有了前面的基础,后面就是将页面展示出来. 预览图如下:1号和31号分别有活动,会一并显示出来   这里需要搞定几个问题,一个就是数据库的连接,我们用\sys\class\class.db_connec ...

  5. (转载)delphi 把图片存入数据库

    delphi 把图片存入数据库 procedure TForm1.Button1Click(Sender: TObject); // 插入图片过程var Stream:TMemoryStream;be ...

  6. javax.servlet不存在的问题

    摘自:http://blog.csdn.net/tgeh23/article/details/2216682     最近在学习servlet,看书看的似乎还比较理想就想上机试下,这一试就发现,问题来 ...

  7. 基于jsp+servlet图书管理系统之后台用户信息查询操作

    上一篇的博客写的是插入操作,且附有源码和数据库,这篇博客写的是查询操作,附有从头至尾写的代码(详细的注释)和数据库! 此次查询操作的源码和数据库:http://download.csdn.net/de ...

  8. Python的数据类型总结

    原地可变类型和不可变类型 原地不可变类型又叫可哈希(hashable)类型,原地可变类型又叫不可哈希类型. 原地不可变类型: 数字类型:int, float, decimal.Decimal, fra ...

  9. virtualBox ubuntu 文件共享

    如何将主机中的文件共享到虚拟机中: 1.  查看/dev中的文件  命令:ls /dev 2.  找到 cdrom1 ,直接挂载到/mnt 命令:sudo mount /dev/cdrom1 /mnt ...

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

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