基于hibernate实现的分页技术实例分析

 更新时间:2016年03月24日 11:40:54   作者:beyond667  
这篇文章主要介绍了基于hibernate实现的分页技术,结合实例形式分析了Hibernate分页技术的原理,实现步骤与相关实现技巧,需要的朋友可以参考下

本文实例讲述了基于hibernate实现的分页技术。分享给大家供大家参考,具体如下:

先说明一下基于hibernate实现分页的原理,假如从数据库取出100条数据,我们要让每页显示10条,假如从30开始,只需要设置起始位置和最大的返回结果即可
先上代码:注意传进来的参数有 Page这类,后面有介绍

public List<Article> queryByPage(final String username, final Page page) {
    return this.getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session session)
          throws HibernateException, SQLException {
        Query query = session.createQuery("select art from Article art where art.username = ?");
        //设置参数
        query.setParameter(0, username);
        //设置每页显示多少个,设置多大结果。
        query.setMaxResults(page.getEveryPage());
        //设置起点
        query.setFirstResult(page.getBeginIndex());
        return query.list();
      }
});

上面关键代码是 setMaxResults(),和setFirstResult(),即设置最大显示值和起点

这里我们需要一个Page工具类,用来操作分页。

Page.java:

package com.fenye;
public class Page {
  // 1.每页显示数量(everyPage)
  private int everyPage;
  // 2.总记录数(totalCount)
  private int totalCount;
  // 3.总页数(totalPage)
  private int totalPage;
  // 4.当前页(currentPage)
  private int currentPage;
  // 5.起始点(beginIndex)
  private int beginIndex;
  // 6.是否有上一页(hasPrePage)
  private boolean hasPrePage;
  // 7.是否有下一页(hasNextPage)
  private boolean hasNextPage;
  public Page(int everyPage, int totalCount, int totalPage, int currentPage,
      int beginIndex, boolean hasPrePage, boolean hasNextPage) {
    this.everyPage = everyPage;
    this.totalCount = totalCount;
    this.totalPage = totalPage;
    this.currentPage = currentPage;
    this.beginIndex = beginIndex;
    this.hasPrePage = hasPrePage;
    this.hasNextPage = hasNextPage;
  }
  //构造函数,默认
  public Page(){}
  //构造方法,对所有属性进行设置
  public int getEveryPage() {
    return everyPage;
  }
  public void setEveryPage(int everyPage) {
    this.everyPage = everyPage;
  }
  public int getTotalCount() {
    return totalCount;
  }
  public void setTotalCount(int totalCount) {
    this.totalCount = totalCount;
  }
  public int getTotalPage() {
    return totalPage;
  }
  public void setTotalPage(int totalPage) {
    this.totalPage = totalPage;
  }
  public int getCurrentPage() {
    return currentPage;
  }
  public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
  }
  public int getBeginIndex() {
    return beginIndex;
  }
  public void setBeginIndex(int beginIndex) {
    this.beginIndex = beginIndex;
  }
  public boolean isHasPrePage() {
    return hasPrePage;
  }
  public void setHasPrePage(boolean hasPrePage) {
    this.hasPrePage = hasPrePage;
  }
  public boolean isHasNextPage() {
    return hasNextPage;
  }
  public void setHasNextPage(boolean hasNextPage) {
    this.hasNextPage = hasNextPage;
  }
}

Page工具类主要是封装页面信息,一共多少数据啊,一页显示多少啊,起点的序号,总页数,是否有上一页下一页,当前页。

还需要一个操作page的工具类,PageUtil.java

package com.sanqing.fenye;
/*
 * 分页信息辅助类
 */
public class PageUtil {
  public static Page createPage(int everyPage,int totalCount,int currentPage) {
    everyPage = getEveryPage(everyPage);
    currentPage = getCurrentPage(currentPage);
    int totalPage = getTotalPage(everyPage, totalCount);
    int beginIndex = getBeginIndex(everyPage, currentPage);
    boolean hasPrePage = getHasPrePage(currentPage);
    boolean hasNextPage = getHasNextPage(totalPage, currentPage);
    return new Page(everyPage, totalCount, totalPage, currentPage,
        beginIndex, hasPrePage, hasNextPage);
  }
  public static Page createPage(Page page,int totalCount) {
    int everyPage = getEveryPage(page.getEveryPage());
    int currentPage = getCurrentPage(page.getCurrentPage());
    int totalPage = getTotalPage(everyPage, totalCount);
    int beginIndex = getBeginIndex(everyPage, currentPage);
    boolean hasPrePage = getHasPrePage(currentPage);
    boolean hasNextPage = getHasNextPage(totalPage, currentPage);
    return new Page(everyPage, totalCount, totalPage, currentPage,
        beginIndex, hasPrePage, hasNextPage);
  }
  //设置每页显示记录数
  public static int getEveryPage(int everyPage) {
    return everyPage == 0 ? 10 : everyPage;
  }
  //设置当前页
  public static int getCurrentPage(int currentPage) {
    return currentPage == 0 ? 1 : currentPage;
  }
  //设置总页数,需要总记录数,每页显示多少
  public static int getTotalPage(int everyPage,int totalCount) {
    int totalPage = 0;
    if(totalCount % everyPage == 0) {
      totalPage = totalCount / everyPage;
    } else {
      totalPage = totalCount / everyPage + 1;
    }
    return totalPage;
  }
  //设置起始点,需要每页显示多少,当前页
  public static int getBeginIndex(int everyPage,int currentPage) {
    return (currentPage - 1) * everyPage;
  }
  //设置是否有上一页,需要当前页
  public static boolean getHasPrePage(int currentPage) {
    return currentPage == 1 ? false : true;
  }
  //设置是否有下一个,需要总页数和当前页
  public static boolean getHasNextPage(int totalPage, int currentPage) {
    return currentPage == totalPage || totalPage == 0 ? false : true;
  }
}

创建Page只需要3个参数,每页显示多少数据,当前页,总共多少数据,其他的4个参数都可以通过这三个计算出来

所以后面要创建Page,只需要调用这工具方法PageUtil.createPage(3个参数),就返回一Page.

返回的Page就是前面参数的Page,即要显示的分页

这样就算完成了分页的功能。

希望本文所述对大家基于Hibernate框架的Java程序设计有所帮助。

相关文章

  • 入门Java线程基础一篇就够了

    入门Java线程基础一篇就够了

    线程是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所拥有的全部资源
    2021-06-06
  • SpringBoot项目中如何访问HTML页面

    SpringBoot项目中如何访问HTML页面

    这篇文章主要介绍了SpringBoot项目中如何访问HTML页面,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • 完整的logback配置示例ELK整合包含生成json日志

    完整的logback配置示例ELK整合包含生成json日志

    这篇文章主要为大家介绍了完整的logback配置示例ELK整合包含生成json日志,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-03-03
  • SpringMVC超详细讲解视图和视图解析器

    SpringMVC超详细讲解视图和视图解析器

    这篇文章主要介绍了springMVC中的视图与视图解析器,springMVC视图的种类很多,默认有转发视图和重定向视图,本文就每一种视图给大家详细介绍,需要的朋友可以参考下
    2022-06-06
  • Spring BPP中如何优雅的创建动态代理Bean详解

    Spring BPP中如何优雅的创建动态代理Bean详解

    这篇文章主要给大家介绍了关于Spring BPP中如何优雅的创建动态代理Bean的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-03-03
  • 解决Springboot中Feignclient调用时版本问题

    解决Springboot中Feignclient调用时版本问题

    这篇文章主要介绍了解决Springboot中Feign client调用时版本问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • java 中http请求为了防止乱码解决方案

    java 中http请求为了防止乱码解决方案

    这篇文章主要介绍了java 中http请求为了防止乱码解决方案的相关资料,需要的朋友可以参考下
    2017-02-02
  • java反编译工具Bytecode-Viewer分享

    java反编译工具Bytecode-Viewer分享

    这篇文章主要介绍了java反编译工具Bytecode-Viewer分享,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • java设计模式笔记之适配器模式

    java设计模式笔记之适配器模式

    这篇文章主要为大家详细介绍了java设计模式之适配器模式笔记,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • java实现写入并保存txt文件的代码详解

    java实现写入并保存txt文件的代码详解

    在本篇文章里小编给大家整理了关于java实现写入并保存txt文件的代码实例内容,需要的朋友们可以参考学习下。
    2020-02-02

最新评论