- package com.shop.base;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import com.shop.entity.Pagination;
- /**
- * 分页基类 继承于BaseHibernateDAO类
- * @author Administrator
- *
- */
- public abstract class BasePagination extends BaseHibernateDao {
- /**
- * 分页查询
- * @param page
- * 分页实体类
- * @param sql
- * 分页用sql查询语句
- * 类似:select c.id,c.name,c.age from Customers c order by c.id desc
- * @param Object[] conObjects
- * 查询条件
- */
- @SuppressWarnings("unchecked")
- protected void searchByPagination(Pagination pagination,String hql,Object[] conditions){
- try{
- Session session1 = HibernateSessionFactory.getSession();
- Query query1=session1.createQuery(hql);
- query1=setQuery(query1, conditions);
- query1.setFirstResult((pagination.getPageIndex()-1)*pagination.getPageSize());
- query1.setMaxResults(pagination.getPageSize());
- pagination.setList(query1.list());
- session1.close();
- Session session2 = HibernateSessionFactory.getSession();
- Query query2 = session2.createQuery(hql);
- pagination.setAllRows(countRows(query2, conditions, session2));
- try {
- pagination.setPageCounts();
- } catch (Exception e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- session2.close();
- }catch(RuntimeException re){
- re.printStackTrace();
- }
- }
- /**
- * 设置Query的参数
- * @param query
- * @param conObjects
- * @return
- */
- private Query setQuery(Query query,Object[] conditions){
- if(conditions==null || conditions.length==0){
- return query;
- }
- for (int i = 0; i < conditions.length; i++) {
- query.setParameter(i, conditions[i]);
- }
- return query;
- }
- /**
- * 得到总行数
- * @param query
- * @return
- */
- private int countRows(Query query,Object[] conditions,Session session){
- String hql=query.getQueryString();
- String queryString="";
- if (hql.toUpperCase().indexOf("SELECT")!=-1) {
- int fromIndex=hql.toUpperCase().indexOf("FROM");
- queryString="select Count(*) "+hql.substring(fromIndex+1,hql.length());
- }else {
- queryString="select Count(*) "+hql;
- }
- if (queryString.toUpperCase().indexOf("ORDER")!=-1) {
- queryString=queryString.substring(0,queryString.toUpperCase().indexOf("ORDER"));
- }
- Query rowQuery=session.createQuery(queryString);
- rowQuery=setQuery(rowQuery, conditions);
- rowQuery.setCacheable(true);
- return Integer.parseInt(rowQuery.uniqueResult().toString());
- }
- }
- package com.shop.entity;
- import java.util.List;
- /**
- * 分页实体类
- * @author Administrator
- *
- */
- public class Pagination {
- private int pageIndex;
- private int pageSize;
- private int allRows;
- private int pageCounts;
- private List<Object> list;
- public Pagination(){}
- public Pagination(int pageIndex,int pageSize){
- this.pageIndex=pageIndex;
- this.pageSize=pageSize;
- }
- /**
- * 得到当前页是第几页
- * @return
- */
- public int getPageIndex() {
- return pageIndex;
- }
- /**
- * 设置当前页是第几页
- * @param pageIndex
- */
- public void setPageIndex(int pageIndex) {
- this.pageIndex = pageIndex;
- }
- /**
- * 得到页大小
- * @return
- */
- public int getPageSize() {
- return pageSize;
- }
- /**
- * 设置页大小
- * @param pageSize
- */
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- /**
- * 设置总页数
- * @return
- * @throws Exception
- */
- public void setPageCounts() throws Exception {
- if (pageSize==0) {
- throw new Exception("页尺寸不能为0!");
- }
- if (allRows%pageSize==0) {
- this.pageCounts=allRows/pageSize;
- }else {
- this.pageCounts=allRows/pageSize+1;
- }
- }
- /**
- * 得到总页数
- * @return
- */
- public int getPageCounts(){
- return this.pageCounts;
- }
- /**
- * 得到总行数
- * @return
- */
- public int getAllRows() {
- return allRows;
- }
- /**
- * 设置总行数
- * @param allRows
- */
- public void setAllRows(int allRows) {
- this.allRows = allRows;
- }
- /**
- * 得到分页记录集合
- * @return
- */
- public List<Object> getList() {
- return list;
- }
- /**
- * 设置分页记录集合
- * @param list
- */
- public void setList(List<Object> list) {
- this.list = list;
}
页面控制
-
- <c:if test="${requestScope.pagination.list!=null}">
- <c:set var="page" value="${requestScope.pagination}"/>
- <c:choose>
- <c:when test="${page.pageIndex==1}">
- <span><c:out value="首 页 上一页"/></span>
- </c:when>
- <c:otherwise>
- <a href="mer.do?method=doBrowserSMer&pageIndex=1&" class="blueText"><span class="blueText">首 页</span></a>
- <a href="mer.do?method=doBrowserSMer&pageIndex=${page.pageIndex-1}" class="blueText"><span class="blueText">上一页</span></a>
- </c:otherwise>
- </c:choose>
- <c:choose>
- <c:when test="${page.pageIndex==page.pageCounts || page.pageCounts==0}">
- <span><c:out value=" 下一页 末 页"/></span>
- </c:when>
- <c:otherwise>
- <a href="mer.do?method=doBrowserSMer&pageIndex=${page.pageIndex+1}" class="blueText"><span class="blueText">下一页</span></a>
- <a href="mer.do?method=doBrowserSMer&pageIndex=${page.pageCounts}" class="blueText"><span class="blueText">末 页</span></a>
- </c:otherwise>
- </c:choose>
- </c:if>
- 当前第[<span class="redText">${page.pageIndex}</span>]页
- 共[<span class="redText">${page.pageCounts}</span>]页
- 每页[<span class="redText">${page.pageSize}</span>]条
- 共[<span class="redText">${page.allRows}</span>]条