您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 经营企划 > Hibernate+struts2+spring 实现分页实例
Hibernate+struts2+spring实现分页实例首先定义一个包含分页相关信息的分页类PageResultSet类:packagecn.tutorinfo.pagination;importjava.util.List;/***该类描述了一个分页数据集list中是查询的数据集合,pageInfo则描述了附加的页相关的信息*/publicclassPageResultSetT{privateListTlist;/首先定义一个包含分页相关信息的分页类PageResultSet类:packagecn.tutorinfo.pagination;importjava.util.List;/***该类描述了一个分页数据集list中是查询的数据集合,pageInfo则描述了附加的页相关的信息*/publicclassPageResultSetT{privateListTlist;//当前页的数据信息privatePageInfopageInfo;//当前页的信息publicListTgetList(){returnlist;}publicvoidsetList(ListTlist){this.list=list;}publicPageInfogetPageInfo(){returnpageInfo;}publicvoidsetPageInfo(PageInfopageInfo){this.pageInfo=pageInfo;}}其中关于页的信息类PageInfo定义如下:packagecn.tutorinfo.pagination;/***该类描述了分页记录集中的关于页的信息*/publicclassPageInfo{privateinttotalRow;//总共记录数privateinttotalPage;//总共页数privateintcurrentPage=1;//当前页,默认为1privateintpageSize=20;//页的大小privatebooleanhasPrevious;privatebooleanhasNext;privatebooleanbof;privatebooleaneof;/**构造方法@paramtotalRow总记录数@parampageSize页的大小@parampage页码*/publicPageInfo(inttotalRow,intpageSize,intpage){this.totalRow=totalRow;this.pageSize=pageSize;//根据页大小和总记录数计算出总页数this.totalPage=countTotalPage(this.pageSize,this.totalRow);//修正当前页setCurrentPage(page);init();}publicintgetTotalPage(){returntotalPage;}publicintgetCurrentPage(){returnthis.currentPage;}//修正计算当前页publicvoidsetCurrentPage(intcurrentPage){if(currentPagethis.totalPage){this.currentPage=this.totalPage;}elseif(currentPage1){this.currentPage=1;}else{this.currentPage=currentPage;}}//获取分页大小publicintgetPageSize(){returnpageSize;}//设置分页大小publicvoidsetPageSize(intpageSize){this.pageSize=pageSize;}//获取当前页记录的开始索引publicintgetBeginIndex(){intbeginIndex=(currentPage-1)*pageSize;//索引下标从0开始returnbeginIndex;}//计算总页数publicintcountTotalPage(intpageSize,inttotalRow){inttotalPage=totalRow%pageSize==0?totalRow/pageSize:totalRow/pageSize+1;returntotalPage;}//返回下一页的页码publicintgetNextPage(){if(currentPage+1=this.totalPage){//如果当前页已经是最后页则返回最大页returnthis.totalPage;}returncurrentPage+1;}//返回前一页的页码publicintgetPreviousPage(){if(currentPage-1=1){return1;}else{returncurrentPage-1;}}publicbooleanisHasPrevious(){returnhasPrevious;}publicbooleanisHasNext(){returnhasNext;}publicbooleanisBof(){returnbof;}publicbooleanisEof(){returneof;}publicbooleanhasNext(){returncurrentPagethis.totalPage;}publicbooleanhasPrevious(){returncurrentPage1;}publicbooleanisFirst(){returncurrentPage==1;}publicbooleanisLast(){returncurrentPage=this.totalPage;}//初始化信息privatevoidinit(){this.hasNext=hasNext();this.hasPrevious=hasPrevious();this.bof=isFirst();this.eof=isLast();}}Dao接口层代码:packagecn.tutorinfo.dao;importjava.util.List;importcn.tutorinfo.domain.Teacher;publicinterfaceTeacherDao{//分页查询显示publicListTeacherqueryByPage(Stringhql,intbeginIndex,intpageSize);//查询的结果记录总数publicintqueryRowCount(finalStringhql);}DAO实现层代码:/**分页查询显示*@paramhql查询的的hql语句*@parambeginIndex查询记录的起始索引位置*@parampageSize一次查询记录个数*@returnListTeacher*/@SuppressWarnings(unchecked)publicListTeacherqueryByPage(Stringhql,intbeginIndex,intpageSize){Sessionsession=HibernateSessionFactory.getSessionFactory().getCurrentSession();Transactiontransaction=null;try{transaction=session.beginTransaction();Queryquery=session.createQuery(hql);query.setFirstResult(beginIndex);query.setMaxResults(pageSize);ListTeacherlist=query.list();transaction.commit();returnlist;}catch(Exceptione){if(null!=transaction){transaction.rollback();e.printStackTrace();}}returnnull;}//查询指定HQL的结果记录总数publicintqueryRowCount(finalStringhql){Sessionsession=HibernateSessionFactory.getSessionFactory().getCurrentSession();Transactiontransaction=null;try{transaction=session.beginTransaction();Queryquery=session.createQuery(hql);intresult=query.list().size();//计算个数transaction.commit();returnresult;}catch(Exceptione){if(null!=transaction){transaction.rollback();e.printStackTrace();}}return0;}业务逻辑层接口:packagecn.tutorinfo.service;importjava.util.List;importcn.tutorinfo.domain.Teacher;importcn.tutorinfo.pagination.PageResultSet;publicinterfaceTeacherService{/**@parampagesize每页大小*@parampage要显示数据的页*@return包含结果集和分页信息的数据集*/publicPageResultSetTeacherqueryByPage(intpageSize,intPage);}业务逻辑实现类://分页获取记录值publicPageResultSetTeacherqueryByPage(intpageSize,intpage){Stringhql=FromTeacher;//查询HQL语句inttotalRow=teacherDao.queryRowCount(hql);//计算总记录个数PageInfopageinfo=newPageInfo(totalRow,pageSize,page);//获取该页的记录ListTeacherlist=teacherDao.queryByPage(hql,pageinfo.getBeginIndex(),pageinfo.getPageSize());PageResultSetTeacherpageResultSet=newPageResultSetTeacher();pageResultSet.setList(list);pageResultSet.setPageInfo(pageinfo);returnpageResultSet;}struts2的action中调用业务逻辑实现类来获取页的信息:packagecn.tutorinfo.struts;importcn.tutorinfo.domain.Teacher;importcn.tutorinfo.pagination.PageResultSet;importcn.tutorinfo.service.TeacherService;importcom.opensymphony.xwork2.ActionSupport;publicclassListTeacherActionextendsActionSupport{privatestaticfinallongserialVersionUID=-7735757614996395820L;privateintpage;//接受传递的页码参数privatePageResultSetTeacherpageResultSet;privateTeacherServiceservice;//业务逻辑组件类由Spr
本文标题:Hibernate+struts2+spring 实现分页实例
链接地址:https://www.777doc.com/doc-6077614 .html