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. 【BZOJ3529】【莫比乌斯反演 + 树状数组】[Sdoi2014]数表

    Description 有一张N×m的数表,其第i行第j列(1 < =i < =礼,1 < =j < =m)的数值为 能同时整除i和j的所有自然数之和.给定a,计算数表中不大于 ...

  2. 【CF521C】【排列组合】Pluses everywhere

    Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote ...

  3. 九度OJ 1124 Digital Roots -- 数位拆解

    题目地址:http://ac.jobdu.com/problem.php?pid=1124 题目描述: The digital root of a positive integer is found ...

  4. layerX && layerY

    转载:https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/layerX UIEvent.layerX 非标准 这个属性是非标准的属性,并且 ...

  5. wamp配置

    # #localhost # <VirtualHost *:> DocumentRoot "D:/software/wamp/www" ServerName local ...

  6. sass中常用mixin

    //设置字体大小 @mixin font($s:14px,$h:1.5,$f:microsoft yahei){ font:$s/#{$h} $f; } //参数(方向,大小,颜色),默认:向下,10 ...

  7. CentOs install oracle instant client

    rpm -ivh oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm rpm -ivh oracle-instantclient11.2-de ...

  8. 在hyper安装openwrt

    写了长长长一篇文章,结果把标签关了,这篇文章就不见了,草稿箱也没有!!! 只好直接copy原来作者的文章了 下载地址 openwrt image tulip driver 引用 Want to add ...

  9. c#简单的Json解析类

    使用方法: 引用Newtonsoft.Json.dll文件,然后引用命名空间using Newtonsoft.Json.Linq;JsonDome中有实例,照做就行 现在贴上示例代码 using Ne ...

  10. placeholder属性兼容js支持

    $(function(){ //判断浏览器是否支持placeholder属性 supportPlaceholder='placeholder'in document.createElement('in ...