struts+spring+hibernate通用分页方法.doc_第1页
struts+spring+hibernate通用分页方法.doc_第2页
struts+spring+hibernate通用分页方法.doc_第3页
struts+spring+hibernate通用分页方法.doc_第4页
struts+spring+hibernate通用分页方法.doc_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

分页有2种办法:1、 从数据库中取得记录,在内存中再划分,如果遇到记录数很大的时候效率很成问题,此法不可取。2、 采用hibernate的物理分页,每次只是取一页。从客户端传进来的是第几页和每页多少条记录,要首先查询符合记录的总记录数,再根据总记录数和当前页,每页记录数可以算出要取的是数据库中的第几条记录。2次查询不可避免了。单元测试如下:public final void testFindAllRole() logger.debug(testFindAllRole);long l1=System.currentTimeMillis();String myaction=roleList.do;String curPageNO = 1;CriteriaQuery cq=new CriteriaQuery(Role.class,curPageNO,myaction);cq.setPageSize(10);PageSupport ps=BeanFactory.getInstance().getRightDelegate().findAllRole(cq, state); long l2=System.currentTimeMillis(); AppUtils.printCollection(ps.getResultList(); System.out.println(一共用时为 : +(l2-l1); CriteriaQuery类是对hibernate QBC查询方法的封装,需要的参数是当前操作的实体类,myaction,curPageNO,pageSize,以便构造出相应的上下翻页的工具条。在delegate中只是将值向下传:public PageSupport findAllRole(CriteriaQuery cq, IState state) if (DelegateUtil.isNullParam(cq, CriteriaQuery, state) return null;Request req = new Request();req.setServiceName(ServiceConsts.FindAllRoleProcessor);req.setValue(CriteriaQuery, cq);try Response resp = getDelegate().execute(req);DelegateUtil.setIState(state, resp);return (PageSupport) (resp.getValue(PageSupport); catch (Exception e) DelegateUtil.handleException(e, findAllRole, state);return new PageSupport();在command中public class FindAllRole extends Commandprivate RoleDao dao;public void setDao(RoleDao dao) this.dao = dao;public void execute(Map params, Map response) throws Exceptionresponse.put(PageSupport, (PageSupport)dao.find(CriteriaQuery) params.get(CriteriaQuery),true);直接就是调用dao的find方法:public PageSupport find(final CriteriaQuery cq,final boolean isOffset) return (PageSupport)getHibernateTemplate().execute(new HibernateCallback() public Object doInHibernate(Session session) throws HibernateException, SQLException Criteria criteria = cq.getDetachedCriteria().getExecutableCriteria(session); /得到总行数 int allCounts = (Integer) criteria.setProjection(Projections.rowCount().uniqueResult().intValue(); criteria.setProjection(null);/还原 int curPageNO = PagerUtil.getCurPageNO(cq.getCurPage();/ 当前页 int offset = PagerUtil.getOffset(allCounts, curPageNO, cq.getPageSize(); String toolBar = PagerUtil.getBar(cq.getMyaction(),allCounts,curPageNO,cq.getPageSize(); if(isOffset) criteria.setFirstResult(offset); criteria.setMaxResults(cq.getPageSize(); return new PageSupport(criteria.list(),toolBar,offset,curPageNO); ,true);在后台的整个操作中非常简练,只需要将带上参数的CriteriaQuery传到dao就可以实现。CriteriaQuery封装了hibernate的DetachedCriteria。public class CriteriaQuery implements Serializablepublic CriteriaQuery()super();private static final long serialVersionUID = -1464383406564081554L;/ 初始化listprivate String curPage=null ;private int pageSize=10;private String myaction;private String myform;private CriterionList criterionList;private DetachedCriteria detachedCriteria;public CriteriaQuery(Class c) this.criterionList = new CriterionList();this.detachedCriteria=DetachedCriteria.forClass(c);public CriteriaQuery(Class c,String curPage,String myaction,String myform) this.myaction=myaction;this.myform=myform;this.curPage=curPage;this.criterionList = new CriterionList();this.detachedCriteria=DetachedCriteria.forClass(c);public CriteriaQuery(Class c,String curPage,String myaction) this.myaction=myaction;this.curPage=curPage;this.criterionList = new CriterionList();this.detachedCriteria=DetachedCriteria.forClass(c);public CriterionList getCriterionList() return criterionList;public void setCriterionList(CriterionList criterionList) this.criterionList = criterionList;public String getCurPage() return curPage;public void setCurPage(String curPage) this.curPage = curPage;public DetachedCriteria getDetachedCriteria() return detachedCriteria;public void setDetachedCriteria(DetachedCriteria detachedCriteria) this.detachedCriteria = detachedCriteria;public void add(Criterion c) detachedCriteria.add(c);public void add() for(int i=0;igetCriterionList().size();i+)add(getCriterionList().getParas(i);public void createAlias(String name)detachedCriteria.createCriteria(name);public void createAlias(String name,String value)detachedCriteria.createCriteria(name,value);public Criterion and(CriteriaQuery query, int source, int dest) return Restrictions.and(query.getCriterionList().getParas(source),query.getCriterionList().getParas(dest);public Criterion and(Criterion c,CriteriaQuery query, int source) return Restrictions.and(c, query.getCriterionList().getParas(source);public Criterion and(Criterion c1, Criterion c2) return Restrictions.and(c1,c2);public Criterion or(CriteriaQuery query, int source, int dest) return Restrictions.or(query.getCriterionList().getParas(source),query.getCriterionList().getParas(dest);public Criterion or(Criterion c,CriteriaQuery query, int source) return Restrictions.or(c, query.getCriterionList().getParas(source);public void or(Criterion c1, Criterion c2) this.detachedCriteria.add(Restrictions.or(c1,c2);public void addOrder(String ordername,String ordervalue) / 写入order查询条件if (asc.equals(ordername) detachedCriteria.addOrder(Order.asc(ordervalue); else detachedCriteria.addOrder(Order.desc(ordervalue);public void eq(String keyname,Object keyvalue)criterionList.addPara(Restrictions.eq(keyname, keyvalue);public void notEq(String keyname,Object keyvalue)criterionList.addPara(Restrictions.or(Restrictions.gt(keyname, keyvalue),Restrictions.lt(keyname, keyvalue);public void like(String keyname,Object keyvalue)criterionList.addPara(Restrictions.like(keyname, keyvalue);public void gt(String keyname,Object keyvalue)criterionList.addPara(Restrictions.gt(keyname, keyvalue);public void lt(String keyname,Object keyvalue)criterionList.addPara(Restrictions.lt(keyname, keyvalue);public void le(String keyname,Object keyvalue)criterionList.addPara(Restrictions.le(keyname, keyvalue);public void ge(String keyname,Object keyvalue)criterionList.addPara(Restrictions.ge(keyname, keyvalue);public void ilike(String keyname,Object keyvalue)criterionList.addPara(Restrictions.in(keyname, keyvalue);public void between(String keyname,Object keyvalue1,Object keyvalue2) Criterion c=null;/ 写入between查询条件if (!AppUtils.isBlank(keyvalue1) & !AppUtils.isBlank(keyvalue2) c=Restrictions.between(keyname, keyvalue1, keyvalue2); else if (!AppUtils.isBlank(keyvalue1) c=Restrictions.ge(keyname, keyvalue1); else if (!AppUtils.isBlank(keyvalue2) c=Restrictions.le(keyname, keyvalue2);criterionList.add(c);public int getPageSize() return pageSize;public void setPageSize(int pageSize) this.pageSize = pageSize;public String getMyaction() return myaction;public void setMyaction(String myaction) this.myaction = myaction;public String getMyform() return myform;public void setMyform(String myform) this.myform = myform;另外需要一些辅助类的支持:例如PageSupport 封装了返回的数据和工具条。public class PageSupport implements Serializableprivate int curPageNO;private int offset;private String toolBar;private List resultList=null;public PageSupport() public PageSupport(List resultList,String toolBar,int offset,int curPageNO) this.curPageNO=curPageNO;this.offset=offset;this.toolBar=toolBar;this.resultList=resultList;public List getResultList() return resultList;public void setResultList(List resultList) this.resultList = resultList;public String getToolBar() return toolBar;CriterionList封装了Hibernate的参数:/* * 封装了ArrayList,装的是Criterion */public class CriterionList extends ArrayList public final Criterion getParas(final int index) return (Criterion) super.get(index);public final void addPara(final int index, final Criterion p) super.add(index, p);public final void addPara(final Criterion p) super.add(p);public final int indexofPara(final Criterion p) return super.indexOf(p);public final void removePara(final int index) super.remove(index);PagerUtil用于将客户端的页码和每页多少个记录算出应该取数据库中的记录的位置,再由hibernate构造出一个三层的sql语句。public class PagerUtil /* * * param allCounts 总记录数 * param curPageNO * param pageSize 每页显示的记录数目 * return */public static String getBar(String action,String form,int allCounts,int curPageNO, int pageSize)Pager pager = null;try if (curPageNO (int) Math.ceil(double) allCounts / pageSize) curPageNO =(int) Math.ceil(double) allCounts / pageSize); /得到offset int offset = (curPageNO-1)*pageSize;/从第几条开始取值 /生成工具条 pager=new Pager(allCounts,offset,pageSize); pager.setCurPageNO(curPageNO);/设置当前的页码 catch (Exception e) System.out.println(生成工具条出错!); return pager.getToolBar(action,form);public static String getBar(String url,int allCounts,int curPageNO, int pageSize)Pager pager = null;try if (curPageNO (int) Math.ceil(double) allCounts / pageSize) curPageNO =(int) Math.ceil(double) allCounts / pageSize); /得到offset int offset = (curPageNO-1)*pageSize;/从第几条开始取值 /生成工具条 pager=new Pager(allCounts,offset,pageSize); pager.setCurPageNO(curPageNO);/设置当前的页码 catch (Exception e) System.out.println(生成工具条出错!); return pager.getToolBar(url);public static int getOffset(int rowCounts,int curPageNO, int pageSize)int offset=0;try /得到第几页 if (curPageNO (int) Math.ceil(double) rowCounts / pageSize) curPageNO =(int) Math.ceil(double) rowCounts / pageSize); /得到offset offset = (curPageNO-1)*pageSize; catch (Exception e) System.out.println(getOffset出错!); return offset;public static int getCurPageNO(String curPage)int curPageNO; if (curPage=null|.equals(curPage.trim() curPageNO=1;/第一次处于第一页 else try curPageNO = Integer.parseInt(curPage);/得到当前页 catch (Exception e) curPageNO=1; return curPageNO;public class Pager /private String appName=EnvironmentConfig.getInstance().getPropertyValue(FileConfig.ConfigFile, ApplicationName);private String appName=hwcms;private int curPageNO; / 当前页private int pageSize; / 每页显示的记录数private int rowsCount; / 记录行数private int pageCount; / 页数/* * param allCount 记录行数 * param offset记录开始数目 * param pageSize 每页显示的记录数 */public Pager(int allCount, int offset, int pageSize) this.curPageNO=(offset = 0) ? 1 : (int)Math.ceil(double) offset / pageSize);this.pageSize = pageSize;this.rowsCount = allCount;this.pageCount = (int) Math.ceil(double) allCount / pageSize);public Pager() / getCurPage:返回当前的页数public int getCurPageNO() return curPageNO;/ getPageSize:返回分页大小public int getPageSize() return pageSize;/ getRowsCount:返回总记录行数public int getRowsCount() return rowsCount;/ getPageCount:返回总页数public int getPageCount() return pageCount;/ 第一页public int first() return 1;/ 最后一页public int last() return pageCount;/ 上一页public int previous() return (curPageNO - 1 pageCount) ? pageCount : curPageNO + 1;/ 第一页public boolean isFirst() return (curPageNO = 1) ? true : false;/ 第一页public boolean isLast() return (curPageNO = pageCount) ? true : false;public void setCurPageNO(int curPageNO) this.curPageNO = curPageNO;public String toString() return Pager的值为 + curPageNO = + curPageNO + limit = + pageSize + rowsCount = + rowsCount + pageCount = + pageCount; /* * 获取工具条 不用图片的,用下拉框 * return String */ public String getToolBar(String url) String temp=; if(url.indexOf(?)=-1) temp=?; else temp=&; String str=; str+=; if(isFirst() str+=首页 上一页  else str+=首页  str+=上一页  if(isLast()|rowsCount=0) str+=下一页 尾页  else str+=下一页  str+=尾页  str+= 共+rowsCount+条记录  str+= 转到; int begin=(curPageNO10)?curPageNO-10:1; int end=(pageCount-curPageNO10)?curPageNO+10:pageCount; for(int i=begin;i=end;i+) if(i=curPageNO) str+=第+i+页; else str+=第+i+页; str+=; return str; /* * 获取工具条 * return String */ public String getToolBar(String myaction,String myform) String str = ; str+=+n; str+=function commonSubmit(val)+n; /校验是否全由数字组成 str+=var patrn=/0-91,20$/;+n; str+=if (!patrn.exec(val)+n; str+= alert(请输入有效页号!);+n; str+= return false ;+n; str+= else+n; str+= document.+myform+.action=+myaction+curPageNO=+val;+n; str+= document.+myform+.submit();+n; str+= return true ;+n; str+= +n; str+= +n; str+=+n; str+= 共+rowsCount+条 共+pageCount+页 当前第+curPageNO+页    if(curPageNO=1|curPageNO=0) str+=首页|前页|; else str+=首页|; str+=前页|; if(curPageNO-pageCount=0|pageCount=0|pageCount=1) str+=后页|尾页  else str+=后页|; str+=尾页; if(pageCount=1|pageCount=0) str+=  转到: 页  /str+=;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论