zoukankan      html  css  js  c++  java
  • Java代码实现真分页

    在JavaWeb项目中,分页是一个非常常见且重要的一个小方面。本次作为记载和学习,记录项目中出现的分页并做好学习记录。在这里,用的是SSH框架。框架可以理解如下图:


    在JSP页面,描写的代码如下:

     1 <div align="center">
     2         <c:if test="${page.currentPage>1}">
     3           <a href="show_findStessayAll.action?currentPage=1" >首页</a>
     4            <a href="show_findStessayAll.action?currentPage=${page.currentPage-1 }">上一页</a>
     5         </c:if>
     6         <c:if test="${page.currentPage != page.totalPage }">
     7            <a href="show_findStessayAll.action?currentPage=${page.currentPage+1 }">下一页</a>
     8            <a href="show_findStessayAll.action?currentPage=${page.totalPage}">末页</a>
     9         </c:if>                        
    10                         
    11         <form action="show_findStessayAll.action">
    12                共${page.totalPage}页  
    13            <input type="text" value="${page.currentPage}" name="currentPage" size="1">14            <input type="submit" value="go">
    15         </form>                        
    16</div>    


    Action部分代码:

     1 public String findAcadcommAll(){
     2         //page存储页面数据
     3         Page<Acadcomm> page = new Page<Acadcomm>();
     4         
     5         //总记录数    
     6         int totalRecord = showService.findAcadcommRecord();    
     7         if(totalRecord!=0){
     8             page.setTotalRecord(totalRecord);
     9             //总页数
    10             int totalPage = ( totalRecord % page.getPageSize() == 0) ? totalRecord / page.getPageSize():totalRecord / page.getPageSize()+1;    
    11             page.setTotalPage(totalPage);
    12             //当前页
    13             int currentPage = 1;
    14             String currentPageString = req.getParameter("currentPage");
    15             System.out.println("currentPageString:"+currentPageString);
    16             if(currentPageString != null){
    17                 currentPage = Integer.parseInt(currentPageString);
    18             }
    19             page.setCurrentPage(currentPage);
    20             System.out.println("currentPage:"+currentPage);
    21             
    22             String hql = "from Acadcomm a"; // 查询语句
    23             //要显示的数据
    24             if(totalRecord % page.getPageSize()!=0 && currentPage==totalPage ){
    25                 page.setDataList(showService.queryForPage(acadcomm,hql, (currentPage-1)*page.getPageSize(), totalRecord % page.getPageSize()));
    26         
    27             }else {
    28                 page.setDataList(showService.queryForPage(acadcomm,hql, (currentPage-1)*page.getPageSize(),page.getPageSize()));
    29             }
    30         }
    31         req.setAttribute("page", page);
    32         
    33         return "findAcadcommAllSuccess";
    34     }

    调用Service

    public int findStessayRecord(String hql);//返回总记录数
        public List<Stessay> queryForPage(Stessay stessay, String hql, int offset,
                int length);//返回当前页面数据

    ServiceImpl调用对应Dao里面的函数

     1 @Override
     2     public int findStessayRecord(String hql) {
     3         // TODO Auto-generated method stub
     4         
     5         return stessayDao.getAllRowCount(hql); // 总记录数
     6     }
     7     @Override
     8     public List<Stessay> queryForPage(Stessay stessay, String hql, int offset,
     9             int length) {
    10         // TODO Auto-generated method stub
    11         return stessayDao.queryForPage(hql, offset, length);
    12     }

    Dao接口只有方法,不写具体实现:

    1  public List<Stessay> findAll(); 
    2  public int getAllRowCount(String hql);
    3  public List<Stessay> queryForPage(final String hql, final int offset, final int length) ;

    具体方法的实现交给DaoImpl:

     1 /**   
     2      * 查询所有的记录数   
     3      * @param hql 查询条件   
     4      * @return 总记录数   
     5      */   
     6     @Override
     7     public int getAllRowCount(String hql) {    
     8         return this.getHibernateTemplate().find(hql).size();    
     9     }    
    10     /**   
    11      * 分页查询   
    12      * @param hql  查询条件   
    13      * @param offset  开始记录   
    14      * @param length  一次查询几条记录   
    15      * @return 查询的记录集合   
    16      */    
    17     @Override
    18     public List<Stessay> queryForPage(final String hql, final int offset, final int length) {    
    19         Session session = this.getSession();    
    20         Query q = session.createQuery(hql);    
    21         q.setFirstResult(offset);    
    22         q.setMaxResults(length);    
    23         List<Stessay> list = q.list();
    24         
    25         System.out.println("--------PaperImpl---------------size()  "+list.size());    
    26         session.close();    
    27         return list;    
    28     }   

    真分页就是页面显示多少,就从数据库里加载多少出来,这样一来就会提高效率。假分页就是将数据库里面所有的数据全部加载出来,但是只显示需要显示的部分,对于数据特别多的系统,这样下来,效率就会特别低。

  • 相关阅读:
    实战-rsync+inotify打造文件实时备份
    实战-Mysql5.6.36脚本编译安装及初始化
    实战-CentOS6.8配置nfs服务
    CentOS7操作系统初始化
    docker搭建 SonarQube代码质量管理平台
    ubuntu 教程
    前端图表库
    WebSSH2安装过程可实现WEB可视化管理SSH工具
    devops 自动化平台网址
    AIops 智能运维平台
  • 原文地址:https://www.cnblogs.com/zxcjj/p/7028927.html
Copyright © 2011-2022 走看看