Hibernate 分页基类及分页实体类

  1. package com.shop.base;
  2. import org.hibernate.Query;
  3. import org.hibernate.Session;
  4. import com.shop.entity.Pagination;
  5. /**
  6.  * 分页基类 继承于BaseHibernateDAO类
  7.  * @author Administrator
  8.  *
  9.  */
  10. public abstract class BasePagination extends BaseHibernateDao {
  11.     /**
  12.      * 分页查询
  13.      * @param page
  14.      *      分页实体类
  15.      * @param sql
  16.      *      分页用sql查询语句
  17.      *      类似:select c.id,c.name,c.age from Customers c order by c.id desc
  18.      * @param Object[] conObjects
  19.      *          查询条件
  20.      */
  21.     @SuppressWarnings("unchecked")
  22.     protected void searchByPagination(Pagination pagination,String hql,Object[] conditions){
  23.         try{
  24.             Session session1 = HibernateSessionFactory.getSession();
  25.             Query query1=session1.createQuery(hql);
  26.             query1=setQuery(query1, conditions);
  27.             query1.setFirstResult((pagination.getPageIndex()-1)*pagination.getPageSize());
  28.             query1.setMaxResults(pagination.getPageSize());
  29.             pagination.setList(query1.list());
  30.             session1.close();
  31.             
  32.             Session session2 = HibernateSessionFactory.getSession();
  33.             Query query2 = session2.createQuery(hql);
  34.             pagination.setAllRows(countRows(query2, conditions, session2));
  35.             try {
  36.                 pagination.setPageCounts();
  37.             } catch (Exception e) {
  38.                 // TODO 自动生成 catch 块
  39.                 e.printStackTrace();
  40.             }
  41.             session2.close();
  42.         }catch(RuntimeException re){
  43.             re.printStackTrace();
  44.         }
  45.     }
  46.     
  47.     /**
  48.      * 设置Query的参数
  49.      * @param query
  50.      * @param conObjects
  51.      * @return
  52.      */
  53.     private Query setQuery(Query query,Object[] conditions){
  54.         if(conditions==null || conditions.length==0){
  55.             return query;
  56.         }
  57.         for (int i = 0; i < conditions.length; i++) {
  58.             query.setParameter(i, conditions[i]);
  59.         }
  60.         return query;
  61.     }
  62.     
  63.     /**
  64.      * 得到总行数
  65.      * @param query
  66.      * @return
  67.      */
  68.     private int countRows(Query query,Object[] conditions,Session session){
  69.         String hql=query.getQueryString();
  70.         String queryString="";
  71.         if (hql.toUpperCase().indexOf("SELECT")!=-1) {
  72.             int fromIndex=hql.toUpperCase().indexOf("FROM");
  73.             queryString="select Count(*) "+hql.substring(fromIndex+1,hql.length());
  74.         }else {
  75.             queryString="select Count(*) "+hql;
  76.         }
  77.         if (queryString.toUpperCase().indexOf("ORDER")!=-1) {
  78.             queryString=queryString.substring(0,queryString.toUpperCase().indexOf("ORDER"));
  79.         }
  80.         Query rowQuery=session.createQuery(queryString);
  81.         rowQuery=setQuery(rowQuery, conditions);
  82.         rowQuery.setCacheable(true);
  83.         return Integer.parseInt(rowQuery.uniqueResult().toString());
  84.     }
  85.     
  86. }

 

 

 

 

 

  1. package com.shop.entity;
  2. import java.util.List;
  3. /**
  4.  * 分页实体类
  5.  * @author Administrator
  6.  *
  7.  */
  8. public class Pagination {
  9.     private int pageIndex;
  10.     private int pageSize;
  11.     private int allRows;
  12.     private int pageCounts;
  13.     private List<Object> list;
  14.     public Pagination(){}
  15.     
  16.     public Pagination(int pageIndex,int pageSize){
  17.         this.pageIndex=pageIndex;
  18.         this.pageSize=pageSize;
  19.     }
  20.     /**
  21.      * 得到当前页是第几页
  22.      * @return
  23.      */
  24.     public int getPageIndex() {
  25.         return pageIndex;
  26.     }
  27.     /**
  28.      * 设置当前页是第几页
  29.      * @param pageIndex
  30.      */
  31.     public void setPageIndex(int pageIndex) {
  32.         this.pageIndex = pageIndex;
  33.     }
  34.     /**
  35.      * 得到页大小
  36.      * @return 
  37.      */
  38.     public int getPageSize() {
  39.         return pageSize;
  40.     }
  41.     /**
  42.      * 设置页大小
  43.      * @param pageSize
  44.      */
  45.     public void setPageSize(int pageSize) {
  46.         this.pageSize = pageSize;
  47.     }
  48.     /**
  49.      * 设置总页数
  50.      * @return
  51.      * @throws Exception
  52.      */
  53.     public void setPageCounts() throws Exception {
  54.         if (pageSize==0) {
  55.             throw new Exception("页尺寸不能为0!");
  56.         }
  57.         if (allRows%pageSize==0) {
  58.             this.pageCounts=allRows/pageSize;
  59.             
  60.         }else {
  61.             this.pageCounts=allRows/pageSize+1;
  62.         }
  63.     }
  64.     /**
  65.      * 得到总页数
  66.      * @return
  67.      */
  68.     public int getPageCounts(){
  69.         return this.pageCounts;
  70.     }
  71.     /**
  72.      * 得到总行数
  73.      * @return
  74.      */
  75.     public int getAllRows() {
  76.         return allRows;
  77.     }
  78.     /**
  79.      * 设置总行数
  80.      * @param allRows
  81.      */
  82.     public void setAllRows(int allRows) {
  83.         this.allRows = allRows;
  84.     }
  85.     /**
  86.      * 得到分页记录集合
  87.      * @return
  88.      */
  89.     public List<Object> getList() {
  90.         return list;
  91.     }
  92.     /**
  93.      * 设置分页记录集合
  94.      * @param list
  95.      */
  96.     public void setList(List<Object> list) {
  97.         this.list = list;
  98.     

}

 

 

 

页面控制

    1.                 <c:if test="${requestScope.pagination.list!=null}">
    2.                    <c:set var="page" value="${requestScope.pagination}"/>
    3.                    <c:choose>
    4.                         <c:when test="${page.pageIndex==1}">
    5.                             <span><c:out value="首 页  上一页"/></span>
    6.                         </c:when>
    7.                         <c:otherwise>
    8.                                  <a href="mer.do?method=doBrowserSMer&pageIndex=1&" class="blueText"><span class="blueText">首 页</span></a> 
    9.                                  <a href="mer.do?method=doBrowserSMer&pageIndex=${page.pageIndex-1}" class="blueText"><span class="blueText">上一页</span></a> 
    10.                         </c:otherwise>
    11.                    </c:choose>
    12.                     <c:choose>
    13.                         <c:when test="${page.pageIndex==page.pageCounts || page.pageCounts==0}">
    14.                             <span><c:out value=" 下一页 末 页"/></span>
    15.                         </c:when>
    16.                         <c:otherwise>
    17.                             <a href="mer.do?method=doBrowserSMer&pageIndex=${page.pageIndex+1}" class="blueText"><span class="blueText">下一页</span></a> 
    18.                             <a href="mer.do?method=doBrowserSMer&pageIndex=${page.pageCounts}" class="blueText"><span class="blueText">末 页</span></a> 
    19.                         </c:otherwise>              
    20.                     </c:choose>
    21.         </c:if>
    22.                      当前第[<span class="redText">${page.pageIndex}</span>]页
    23.                      共[<span class="redText">${page.pageCounts}</span>]页 
    24.                      每页[<span class="redText">${page.pageSize}</span>]条
    25.                      共[<span class="redText">${page.allRows}</span>]条  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值