Java项目中基于Hibernate分页总结
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分页总结的更多相关文章
- java环境中基于jvm的两大语言:scala,groovy
一.java环境中基于jvm的两大语言:scala,groovy 可以在java项目里混编这两种语言: scala:静态语言,多范式语言,糅合了面向对象.面向过程:可以与java和net互操作:融汇了 ...
- 在Java项目中整合Scala
Scala是一个运行在Java JVM上的面向对象的语言.它支持函数编程,在语法上比Java更加灵活,同时通过Akka库,Scala支持强大的基于Actor的多线程编程.具有这些优势,使得我最近很想在 ...
- XML在JAVA项目中的作用
java项目中,xml文件一般都是用来存储一些配置信息 一般的编程, 多数用来存储配置信息 . 拿JDBC来说,可以把数据库连接字符串写到xml,如果要修改数据源,只需要改xml就可以了,没必要再去重 ...
- ckeditor编辑器在java项目中配置
一.基本使用: 1.所需文件架包 A. Ckeditor基本文件包,比如:ckeditor_3.6.2.zip 下载地址:http://ckeditor.com/download 2.配置使用 A.将 ...
- Ant在Java项目中的使用(一眼就看会)
参考:http://www.cnblogs.com/zhengqiang/p/5557155.html Ant是跨平台的构建工具,它可以实现项目的自动构建和部署等功能.在本文中,主要让读者熟悉怎样将A ...
- JAVA 项目中使用 H2 数据库
为什么要使用H2数据库 H2数据库是可以嵌入到JAVA项目中的,因为只需要导入一个jar包即可,所以非常的方便. 项目中导入H2 将H2的jar包放到classpath里即可,我是用的maven,ma ...
- UCenter在JAVA项目中实现的单点登录应用实例
Comsenz(康盛)的UCenter当前在国内的单点登录领域占据绝对份额,其完整的产品线令UCenter成为了账号集成方面事实上的标准. 基于UCenter,可以将Comsenz旗下的Discuz! ...
- 实战派 | Java项目中玩转Redis6.0客户端缓存!
原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 哈喽大家好啊,我是Hydra. 在前面的文章中,我们介绍了Redis6.0中的新特性客户端缓存client-side caching,通过tel ...
- 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. ...
随机推荐
- IOS 学习日志 2015-3-17
Objective--C 一 关键字 @class 导入已有的类 id 对象类型 表示任何一个ObjC对象类型 Block 对象类型 OC中称为代码块 类似于C中的函数式指针 typedef 定义数据 ...
- 专题一、ArrayList增删操作技术细节详解
一.索引检查 1)在指定位置插入元素时,第一步都需要检查输入的指定位置是否合法 public void add(int index, E element){ rangeCheckForAdd(i ...
- Ubuntu 之旅—— 调整扩展屏分辨率
打开终端输入 xrandr 得到如下信息 Screen 0: minimum 320 x 200, current 2390 x 768, maximum 8192 x 8192 LVDS conne ...
- find命令使用, -exec xargs
find [path] [expression] 例如:find /home -name \*.o -exec rm '{}' \; find: 实时精确,支持众多查找标准,遍历指定目录中 ...
- jquery 放大图片
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- java中运算符——进度1
Class Demo1{ public static void main(String[] args) { /* 一.逻辑运算法用于连接两个boolean类型的表达式 ...
- Windows加密视频播放器使用教程
1. 下载文件 http://pan.baidu.com/s/1c2aESQs 2. 操作流程 温馨提示 播放时,请务必保证播放设备联网(原因:用户名权限验证需要网络,播放后10秒即可关闭网 ...
- 表格行变换顺序功能(jquery)
周末写了个更改表格行顺序的小功能,直接贴代码 表格部分如下: <table class="table" id="test_table"> <t ...
- Servlet 的由来和实现过程
Servlet 是在服务器上运行的小程序.这个词是在 Java applet的环境中创造的,Java applet 是一种当作单独文件跟网页一起发送的小程序,它通常用于在客户端运行,结果 ...
- CSS3 学习
border-radius: 半径,不用学了,用得很熟了,但要记得它的某一个角的写法是border-top/bottom-left/right-radius: ,参数中的两个值为先左右后上下,支持百分 ...