文档介绍:该【【精品】浅谈分页思路 】是由【mama1】上传分享,文档一共【7】页,该文档可以免费在线阅读,需要了解更多关于【【精品】浅谈分页思路 】的内容,可以使用淘豆网的站内搜索功能,选择自己适合的文档,以下文字是截取该文章内的部分文字,如需要获得完整电子版,请下载此文档到您的设备,方便您编辑和打印。【精品】浅谈分页思路浅谈分页思路鄙人不才,最近做了个用到分页的项目,经过一番推敲,现在把思路跟大家讲解下:先声明这些都是自己的见解有不到之处还望大牛多多指教。我们常听说的有两种分页方式(以讹传讹的方式听说的):1、真分页2、假分页真分页:每次向数据库中查询肯定条数的数据。假分页:一次性查询出全部的数据存放在容器中,然后从容器中取出肯定条数的数据。本文将讲解真分页的实现思路:废话不多说上代码:首先新建一个分页的实体类:.entity;lassPage{privateintupPage;//上一页privateintdownPage;//下一页privateintnowPage;//当前页privateintheadPage;//首页----当前页=1的时候privateintfootPage;//尾页----总页数privateintpageNum=5;//每页显示的数据条数publicintgetUpPage(){returnupPage;}}publicvoidsetUpPage(intupPage){=upPage;}publicintgetDownPage(){returndownPage;}publicvoidsetDownPage(intdownPage){=downPage;}publicintgetNowPage(){returnnowPage;}publicvoidsetNowPage(intnowPage){=nowPage;}publicintgetHeadPage(){returnheadPage;}publicvoidsetHeadPage(intheadPage){=headPage;}publicintgetFootPage(){returnfootPage;}publicvoidsetFootPage(intfootPage){=footPage;}publicintgetPageNum(){returnpageNum;}publicvoidsetPageNum(intpageNum){=pageNum;}publicPage(){super();}.;;;;;;;.;.;.;lassTestPageDao{privateConnectionconn;privatePreparedStatementpsmt;privateResultSetrs;//nullpublicListPlotqueryAll(Pagepage){ListPlotlist=newArrayListPlot();Stringsql=select*from(selectrownumasr,t.*+from(select*fromplotwhererownum=?)t)wherer?;conn=();//这里获得数据库连接建议运用数据库连接池try{()*());(2,(()-1)*5);rs=();while(()){psmt=(sql);(1,Stringplot_ID=(2);//为什么是从2起先?而不是从1起先呢?执行上面语句会给查询结果一个编号//序列,第一列是编号。其次列才是我们想要的数据。Stringlongitude=(3);Stringlatitude=(4);Stringdescribe=(5);Plotplot=newPlot(plot_ID,longitude,latitude,describe);(plot);}}/*}catch(SQLExceptione){();}returnlist;*获得表中一共有多少条数据*/publicintcountPage(){Stringsql=selectplot_idfromplot;conn=();try{psmt=(sql);rs=();inti=0;while(()){i++;}returni;}catch(SQLExceptione){}return0;();}}Servlet:.servlet;;;;..;.;lassTestPageServletextendsHttpServlet{privatestaticfinallongserialVersionUID=1L;publicTestPage(){super();}protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{Stringflag=(flag);Stringnow=(now);Pagepage=newPage();TestPageDaodao=newTestPageDao();intcount=();inttemp=count/()+1;if(count%()==0){temp=count/();}(temp);switch(flag){casehead:caseup:if(now==1){(1);}else{(1);break;casedown:if(now==temp){(temp);}else{(now+1);}break;casefoot:(now-1);}break;}(temp);break;equest,}protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doGet(request,response);}}ListPlotlist=(page);(list,list);(page,page);().forward(rreturn;response);JSP:%***@pagelanguage=javacontentType=text/html;charset=UTF-8pageEncoding=UTF-8%%***@taglibprefix=curi=/jsp/jstl/core%!DOCTYPEhtmlPUBLIC-//W3C////;;;;;;;lassDBConnect{onnectionconn;(Context)(java:/comp/env);DataSourceds=(DataSource)(OracleConn);conn=();}catch(NamingExceptione){();}catch(SQLExceptione){();}returnconn;}}onnectiongetConn(){ContextinitContext;try{initContext=newInitialContext();ContextenvContext=附件文件3:.entity;lassPlot{privateStringplot_ID;privateStringlongitude;privateStringlatitude;privateStringdescribe;publicStringgetPlot_ID(){returnplot_ID;}publicvoidsetPlot_ID(Stringplot_ID){=plot_ID;}publicStringgetLongitude(){returnlongitude;}publicvoidsetLongitude(Stringlongitude){=longitude;}publicStringgetLatitude(){returnlatitude;}publicvoidsetLatitude(Stringlatitude){=latitude;latitude,}}}publicStringgetDescribe(){returndescribe;}publicvoidsetDescribe(Stringdescribe){=describe;}publicPlot(){super();//TODOAuto-generatedconstructorstub}publicPlot(Stringplot_ID,Stringlongitude,Stringsuper();=plot_ID;=longitude;=latitude;=describe;Stringdescribe){到这里基本就可以实现一个简洁的分页了;比如每页显示5条数据。在第一页的时候。当我点击下一页的时候,记录当前的页面为nowPage。那么就向数据库中去查第nowPage*pageNum到(nowPage+1)*pageNum条数据。上一下同理。下面说几个留意点:1、这个jsp页面运用了jstl标签来进行循环输出从servlet转发过来的list。运用时,记得导入jstl包;2、数据库运用的是orcale数据库,其他数据库类似。实现方式都是一样的。