一个非常强大的SSH分页方法
分页基本上是每个项目都会用到的模块。在这里我总结了一下网上主流的分页模式,自己做了一套。跟大家分享下
?
这个分页方法可以完成对任意表,任意查询条件的查询和分页。基本上可以代替项目中所有的数据库查询功能,当然只限于简单逻辑上的。初步整理,有什么不妥的地方还望大家指出、探讨。
?
首先是分页实体类:
?
view plain- package?engine.entity;????
- import?java.util.List;????
- public?class?Pager?{??????/**?
- ?????*??是否有上一页???????*/??
- ????private?boolean?hasPrePage;??????/**?
- ?????*?是否有下一页???????*/??
- ????private?boolean?hasNextPage;??????/**?
- ?????*??每页的数量???????*/??
- ????private?int?everyPage;??????/**?
- ?????*??总页数??????*/??
- ????private?int?totalPage;??????/**?
- ?????*?当前页???????*/??
- ????private?int?currentPage;???????/**?
- ?????*??起始点???????*/??
- ????private?int?beginIndex;???????/**?
- ?????*??总记录数??????*/??
- ????private?int?totalCount;???????/**?
- ?????*?该页名称??????*/??
- ????private?String?pageName;???????/**?
- ?????*?查询条件??????*/??
- ????private?String?conString;??????public?String?getConString()?{??
- ????????return?conString;??????}??
- ????/**??????*?设置查询条件?
- ?????*/??????public?void?setConString(String?conditionString)?{??
- ????????this.conString?=?conditionString;??????}??
- ????/**??????*?设置该页名称(即查询表名)?
- ?????*/??????public?String?getPageName()?{??
- ????????return?pageName;??????}??
- ??????public?void?setPageName(String?tableName)?{??
- ????????this.pageName?=?tableName;??????}??
- ??????public?int?getTotalCount()?{??
- ????????return?totalCount;??????}??
- ????/**??????*??设置总记录数?
- ?????*/??????public?void?setTotalCount(int?totalCount)?{??
- ????????this.totalCount?=?totalCount;??????}??
- ????//?construct?the?page?by?everyPage???????public?Pager(int?everyPage){??
- ????????this.everyPage?=?everyPage;??????}??
- ????//The?whole?constructor???????public?Pager(boolean?hasPrePage,?boolean?hasNextPage,???
- ????????????????????int?everyPage,?int?totalPage,??????????????????????int?currentPage,?int?beginIndex,int?totalCount,??
- ????????????????????String?pageName,String?conString)?{??????????this.hasPrePage?=?hasPrePage;??
- ????????this.hasNextPage?=?hasNextPage;??????????this.everyPage?=?everyPage;??
- ????????this.totalPage?=?totalPage;??????????this.currentPage?=?currentPage;??
- ????????this.beginIndex?=?beginIndex;??????????this.totalCount?=?totalCount;??
- ????????this.pageName?=?pageName;??????????this.conString?=?conString;??
- ????}??????public?int?getBeginIndex()?{??
- ????????return?beginIndex;??????}??
- ????/**??????*??设置起始点??
- ?????*/??????public?void?setBeginIndex(int?beginIndex)?{??
- ????????this.beginIndex?=?beginIndex;??????}??
- ????public?int?getCurrentPage()?{??????????return?currentPage;??
- ????}??????/**?
- ?????*?设置当前页???????*/??
- ????public?void?setCurrentPage(int?currentPage)?{??????????this.currentPage?=?currentPage;??
- ????}??????public?int?getEveryPage()?{??
- ????????return?everyPage;??????}??
- ????/**??????*??设置每页的数量??
- ?????*/??????public?void?setEveryPage(int?everyPage)?{??
- ????????this.everyPage?=?everyPage;??????}??
- ????/**??????*?是否有下一页??
- ?????*/??????public?boolean?getHasNextPage()?{??
- ????????return?hasNextPage;??????}??
- ????public?void?setHasNextPage(boolean?hasNextPage)?{??????????this.hasNextPage?=?hasNextPage;??
- ????}??????/**?
- ?????*?是否有上一页???????*/??
- ????public?boolean?getHasPrePage()?{??????????return?hasPrePage;??
- ????}??????public?void?setHasPrePage(boolean?hasPrePage)?{??
- ????????this.hasPrePage?=?hasPrePage;??????}??
- ????public?int?getTotalPage()?{??????????return?totalPage;??
- ????}??????/**?
- ?????*??设置总页数??????*/??
- ????public?void?setTotalPage(int?totalPage)?{??????????this.totalPage?=?totalPage;??
- ????}????
- }??
?
然后创建分页结果集(将分页结果打包,便于访问):
?
view plain- package?engine.entity;????
- import?java.util.List;????
- public?class?Result?{??????private?Pager?pager;????
- ????private?List?content;??????
- ?????????//The?default?constructor???????????public?Result()?{??
- ????????????super();??????????}???
- ?????????/**???????????*?@param?分页信息?
- ??????????*?@param?每页显示的集合???????????*/??
- ????????public?Result(Pager?pager,?List?content)?{??????????????this.pager?=?pager;??
- ????????????this.content?=?content;??????????}??
- ???????????
- ????????public?List?getContent()?{??????????????return?content;??
- ????????}????
- ?????????????????public?Pager?getPager()?{??
- ????????????return?pager;??????????}??
- ???????????
- ????????public?void?setContent(List?content)?{??????????????this.content?=?content;??
- ????????}????
- ?????????????????public?void?setPager(Pager?pager)?{??
- ????????????this.pager?=?pager;??????????}??
- }??
?
构建一个page的工厂PageUtil(处理分页相关计算):
?
view plain- package?engine;????
- import?engine.entity.Pager;????
- public?class?PagerUtil?{??????//?Use?the?origin?page?to?create?a?new?page??
- ????
- ????public?static?Pager?createPage(Pager?page,?int?totalRecords)?{??????????return?createPage(page.getEveryPage(),?page.getCurrentPage(),page.getPageName(),??
- ????????????????page.getConString(),totalRecords);??????}??
- ????
- ????//the?basic?page?utils?not?including?exception?handler????
- ??????public?static?Pager?createPage(int?everyPage,?int?currentPage,??
- ????????????String?pageName,String?conString,int?totalRecords)?{??????????everyPage?=?getEveryPage(everyPage);??
- ????????currentPage?=?getCurrentPage(currentPage);??????????int?beginIndex?=?getBeginIndex(everyPage,?currentPage);??
- ????????int?totalPage?=?getTotalPage(everyPage,?totalRecords);??????????boolean?hasNextPage?=?hasNextPage(currentPage,?totalPage);??
- ????????boolean?hasPrePage?=?hasPrePage(currentPage);??????????System.out.println(everyPage+"*"+totalPage+"*"+??
- ????????????????currentPage+"*"+beginIndex+"*"+totalRecords);??????????return?new?Pager(hasPrePage,?hasNextPage,?everyPage,?totalPage,??
- ????????????????currentPage,?beginIndex,?totalRecords,?pageName,?conString);??????}??
- ??????private?static?int?getEveryPage(int?everyPage)?{??
- ????????return?everyPage?==?0???10?:?everyPage;??????}??
- ??????private?static?int?getCurrentPage(int?currentPage)?{??
- ????????return?currentPage?==?0???1?:?currentPage;??????}??
- ??????private?static?int?getBeginIndex(int?everyPage,?int?currentPage)?{??
- ????????return?(currentPage?-?1)?*?everyPage;??????}??
- ??????private?static?int?getTotalPage(int?everyPage,?int?totalRecords)?{??
- ????????int?totalPage?=?0;????????
- ????????if?(totalRecords?%?everyPage?==?0)??????????????totalPage?=?totalRecords?/?everyPage;??
- ????????else??????????????totalPage?=?totalRecords?/?everyPage?+?1;??
- ??????????????return?totalPage;??
- ????}????
- ????private?static?boolean?hasPrePage(int?currentPage)?{??????????return?currentPage?==?1???false?:?true;??
- ????}????
- ????private?static?boolean?hasNextPage(int?currentPage,?int?totalPage)?{??????????return?currentPage?==?totalPage?||?totalPage?==?0???false?:?true;??
- ????}????
- }??
?
数据访问层接口:
?
view plain- package?hibernate.dao;????
- import?java.util.List;????
- import?engine.entity.Pager;????
- public?interface?PagerProductDAO?{??????public?void?setPager(Pager?pager);??
- ????/**??????*?@return?分页后的数据?
- ?????*/??????public?List?getProductByPage();???
- ????/**??????*?不使用分页?
- ?????*?@return?所有符合条件的数据??????*/??
- ????public?List?getProducts();??????/**?
- ?????*?@return?数据的总数??????*/??
- ????public?int?getProductCount();????}??
?
数据访问层接口实现:
?
view plain- package?hibernate.dao;????
- import?java.sql.SQLException;??import?java.util.List;??
- ??import?org.hibernate.HibernateException;??
- import?org.hibernate.Query;??import?org.hibernate.Session;??
- import?org.springframework.orm.hibernate3.HibernateCallback;??import?org.springframework.orm.hibernate3.support.HibernateDaoSupport;??
- ??import?engine.entity.Pager;??
- ????
- public?class?PagerProductDAOImpl?extends?HibernateDaoSupport?implements?PagerProductDAO?{????
- ????Pager?pager;??????public?Pager?getPager()?{??
- ????????return?pager;??????}??
- ????public?void?setPager(Pager?page)?{??????????this.pager?=?page;??
- ????}??????public?List?getProductByPage()?{??
- ????????//?TODO?Auto-generated?method?stub??????????return?getHibernateTemplate().executeFind(new?HibernateCallback(){??
- ????????????public?Object?doInHibernate(Session?session)?throws?HibernateException,?SQLException?{??????????????????Query?query=session.createQuery("from?"+pager.getPageName()+"?where?"+pager.getConString());??
- ????????????????query.setFirstResult(pager.getBeginIndex());?//hibernate分页的精髓??????????????????query.setMaxResults(pager.getEveryPage());??
- ????????????????return?query.list();??????????????}??
- ????????????});??????}??
- ????public?List?getProducts(){??????????return?getHibernateTemplate().find("from?"+pager.getPageName()+"?where?"+pager.getConString());??
- ??????????????}??
- ????public?int?getProductCount()?{??????????//?TODO?Auto-generated?method?stub??
- ????????List?list=getHibernateTemplate().find("from?"+pager.getPageName()+"?where?"+pager.getConString());??????????return?((Integer)list.size()).intValue();??
- ??????}??
- ??}??
?
业务层接口:
?
view plain- package?engine;????
- import?engine.entity.Pager;??import?engine.entity.Result;??
- ??public?interface?PagerProduct?{??
- ????public?Result?listProduct(Pager?pager);??}??
?
业务层接口实现:
?
view plain- package?engine.impl;????
- import?java.util.List;????
- import?engine.PagerProduct;??import?engine.PagerUtil;??
- import?engine.entity.Pager;??import?engine.entity.Result;??
- import?hibernate.dao.PagerProductDAO;????
- public?class?PagerProductImpl?implements?PagerProduct?{????
- ????private?PagerProductDAO?pagerProductDAO;??????public?PagerProductDAO?getPagerProductDAO()?{??
- ????????return?pagerProductDAO;??????}??
- ????public?void?setPagerProductDAO(PagerProductDAO?pagerProductDAO)?{??????????this.pagerProductDAO?=?pagerProductDAO;??
- ????}??????public?Result?listProduct(Pager?pager)?{??
- ????????//?TODO?Auto-generated?method?stub??????????this.pagerProductDAO.setPager(pager);??
- ????????List?products=null;??????????if(pager.getEveryPage()==0){??
- ????????????//不使用分页??????????????products?=?this.pagerProductDAO.getProducts();??
- ????????}??????????else{??
- ????????????int?totalRecords?=?this.pagerProductDAO.getProductCount();??????????????pager?=?PagerUtil.createPage(pager,?totalRecords);??
- ????????????//载入新生产的page??????????????this.pagerProductDAO.setPager(pager);??
- ????????????products?=?this.pagerProductDAO.getProductByPage();???????????????}??
- ????????return?new?Result(pager,?products);??????}??
- ??????}??
?
呼~~终于到productAction啦
?
?
view plain- public?class?PlaceAction?extends?DispatchAction?{????????
- ????private?PagerProduct?pagerProduct;??????public?PagerProduct?getPagerProduct()?{??
- ????????return?pagerProduct;??????}??
- ??????public?void?setPagerProduct(PagerProduct?pagerProduct)?{??
- ????????this.pagerProduct?=?pagerProduct;??????}??
- ??????public?ActionForward?find(ActionMapping?mapping,?ActionForm?form,??
- ????????????HttpServletRequest?request,?HttpServletResponse?response)??????????????throws?Exception?{??
- ????????Pager?pager=new?Pager(10);//设置每页10条记录。若不想使用分页设为0即可??????????String?tableName="Place";//查询Place表??
- ????????pager.setPageName(tableName);??????????String?conditionString="Place_id>0";//设置查询条件即sql中where后面的语句??
- ????????pager.setConString(conditionString);??????????int?pageNum=1;//当前显示第1页??
- ????????pager.setCurrentPage(pageNum);??????????Result?result?=?pagerProduct.listProduct(pager);??
- ????????//获取新的page,此时的page已经包所有的信息??????????pager?=?result.getPager();??
- ????????request.setAttribute("PlaceView",?result);??????????return?mapping.findForward("PlaceView");??
- ????}??}??
在jsp页面中
?
view plain- <%@?page?language="java"?pageEncoding="gb2312"%>????
- <%@?taglib?uri="http://struts.apache.org/tags-bean"?prefix="bean"?%>??<%@?taglib?uri="http://struts.apache.org/tags-logic"?prefix="logic"?%>??
- ??<bean:define?id="list"?name="PlaceView"?property="content"?type="java.util.List"></bean:define>??
- <bean:define?id="pager"?name="PlaceView"?property="pager"?type="engine.entity.Pager"></bean:define>??<bean:define?id="hasNextPage"?name="pager"?property="hasNextPage"?type="java.lang.Boolean"></bean:define>??
- <bean:define?id="hasPrePage"?name="pager"?property="hasPrePage"?type="java.lang.Boolean"></bean:define>????????????????
- ????????<logic:empty?name="list">??????????????<div?id="empty">????
- ????????????????<table?border="0">??????????????????<tr>??
- ????????????????????<td?colspan="6">没有可以显示的数据</td>??????????????????</tr>??
- ????????????????</table>??????????????</div>??
- ????????</logic:empty>??????????<logic:notEmpty?name="list">??
- ????????????<logic:iterate?id="i"?name="list"?>???????????????<div?id="place_${i.placeId?}">????
- ????????????????<table?border="0">??????????????????<tr>??
- ????????????????????<td>??????????????????????????<input?type="checkbox"?name="selectFlag"?value="${i.placeId?}">??
- ????????????????????</td>??????????????????????<td>${i.placeId?}</td>????
- ????????????????????<td>${i.placeName?}</td>??????????????????????<td>${i.placeNotes?}</td>??
- ????????????????</tr>????????????????????</table>???
- ??????????????</div>??????????????</logic:iterate>????
- ????????</logic:notEmpty>????????<logic:equal?value="false"?name="hasPrePage">??
- ????????????<span>[上一页]</span>????????</logic:equal>??
- ??????<logic:equal?value="true"?name="hasPrePage"?>??????????????<span><a?href="javascript:changePage(${pager.currentPage-1?})"?mce_href="javascript:changePage(${pager.currentPage-1?})">[上一页]</a></span>??
- ??????</logic:equal>????????${pager.currentPage?}/${pager.totalPage?}??
- ??????<logic:equal?value="false"?name="hasNextPage">??????????????<span>[下一页]</span>??
- ??????</logic:equal>??????????????<logic:equal?value="true"?name="hasNextPage"?>??
- ????????????<span><a?href="javascript:changePage(${pager.currentPage+1?})"?mce_href="javascript:changePage(${pager.currentPage+1?})">[下一页]</a></span>????????</logic:equal>??
?
这样所有的分页功能就完成了,至于changePage()这个JavaScript函数就是处理翻页的动作啦,大家应该都知道了吧,就不多说了。
?
?
?
转载From:http://blog.csdn.net/hytfly/article/details/5787462