分页的实现步骤.doc_第1页
分页的实现步骤.doc_第2页
分页的实现步骤.doc_第3页
分页的实现步骤.doc_第4页
分页的实现步骤.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

为什么需要分页?1. 从客户角度来讲,数据内容过多,查看数据非常不便。2. 从服务器和网络的角度来讲,查这么多数据,传输这么多数据,是一种效率很低的做法。分页的核心SQL:1. mysql-limitSelect * from article limit 4,10;-取第一页的数据:limit 0,10-取第二页的数据:limit 10,10-取第三页的数据:limit 20,10-基本的过程:-1.用户点击页号发送请求:channel.jsp?num=2-2.服务器收到请求后,根据num参数的值,计算记录的起始索引:(num-1)*size-3.调用dao中的方法,该方法发送sql:select * from tb_forum_article limit 10,10;-4.根据dao返回的结果集内容,该内容封装到List中-5.将list中的内容展示到页面上2. oracle -rownumselect r,ename,sal from (select rownum r, ename,sal from (select * from emp order by sal desc) t2 ) where r=5 and r10 limit ?,?;, ps.setint(num-1)*10),返回一个List,传递到jsp中进行显示,发送给客户端。1. 在Dao中增加方法:public static List getArticles(int cid,int startRow,int size)String sql = select tb_forum_article.id,uname username,createTime,count,title +from tb_forum_article +join tb_user on tb_forum_article.userId=tb_user.id +where tb_forum_article.cid=? and tb_forum_article.type!=2 + limit ?,?; /小频道下所有的帖子String sql2 = select tb_forum_article.id,uname username,createTime,count,tb_forum_article.title +from tb_forum_article +join tb_user on tb_forum_article.userId=tb_user.id + join tb_forum_channel on tb_forum_article.cid=tb_forum_channel.id + +where tb_forum_channel.pid=? and tb_forum_article.type!=2 + limit ?,?; /大频道下所有的帖子return MyHib.selectRows如果对jdbc很熟了,可以使用我的封装。如果不熟,仍然手写!(DBUtil.getMysqlConn(), isMainChannel(cid)?sql2:sql, new Objectcid,startRow,size, ArticleInfo.class);2. 在JSP中调用Dao中的分页方法: 第$i页 第$i页 此处循环显示list中的内容。3. 访问:channel.jsp,然后点击在下面的页号导航即可看到简单的分页效果。首页 上一页 1,2,3,4,5,6,7,8,9,10 下一页 末页 共101页 分页的实现原理: 1. 获得需要显示的总的记录数rowCount从数据库中取2. 设定每页最多显示的记录数size103. 指定显示的页码:num 作为参数得到4. 所要显示的数据对象根据startRow和size从数据库中查出!5. 根据rowCount,size,num可计算出其余的元素: a) 本页面从第几个记录开始:startRow = (this.num-1) * size; b) 共有多少页:pageCount = (int) Math.ceil(double)rowCount/size); c) list:包含了所要显示的数据对象的集合d) 下一页:next=Math.min( this.pageCount, this.num+1) e) 上一页:prev = Math.max(1 , this.num-1) f) 页号控制元素: numCount:每页最多显示多少页号。(一共显示numCount+1个页号) start = Math.max(this.num-numCount/2, first); /本页显示页号从多少页开始end = Math.min(start+numCount, last); /本页显示页号在多少页结束 页号控制: if(end-start numCount) /当本页总显示的页号数不够numCount时,如何计算起始页号。 start = Math.max(end-numCount, 1);分页实现步骤Pagenation工具类代码:public class Pagenation private int num; /当前页号, 采用自然数计数 1,2,3,.private int size; /页面大小:一个页面显示多少个数据private long rowCount;/数据总数:一共有多少个数据private int pageCount; / 页面总数private int startRow;/当前页面开始行, 第一行是0行private int first = 1;/第一页 页号private int last;/最后页 页号private int next;/下一页 页号private int prev;/前页 页号private int start;/页号式导航, 起始页号private int end;/页号式导航, 结束页号private int numCount = 10;/页号式导航, 最多显示页号数量为numCount+1;这里显示11页。private List list;public Pagenation(int size, String str_num, long rowCount) int num = 1;if (str_num != null) num = Integer.parseInt(str_num);this.num = num;this.size=size;this.rowCount = rowCount;this.pageCount = (int) Math.ceil(double)rowCount/size);this.num = Math.min(this.num, pageCount);this.num = Math.max(1, this.num);this.startRow = (this.num-1) * size ;this.last = this.pageCount;this.next = Math.min( this.pageCount, this.num+1);this.prev = Math.max(1 , this.num-1);/计算page 控制start = Math.max(this.num-numCount/2, first);end = Math.min(start+numCount, last);if(end-start numCount)start = Math.max(end-numCount, 1);2. jsp中内容: 首页 上一页 首页 上一页 第$i页 第$i页 下一页 末页 下一页 末页 共有$page.pageCount页 主题 回复 作者 阅读 最新文章 $c.title $c.answerNum $c.username $c.count $c.createTime,$c.username,$c.id $c.newArticle.createTime,$c.newArticle.username,$c.newArticle.id 3. dao中的代码参考:public static long getTitleNum(int pid)String sql = select count(*) from tb_forum_article + join tb_forum_channel on tb_forum_article.cid=tb_forum_channel.id + where tb_forum_channel.pid=? and tb_forum_article.type!=2;String sql2 = select count(*) from tb_forum_article where cid=? and type!=2; return (Long) MyHib.selectValue(DBUtil.getMysqlConn(), isMainChannel(pid)?sql:sql2, new Objectpid).longValue();public static List getArticles(int cid,int startRow,int size)String sql = select tb_forum_article.id,uname username,createTime,count,title +from tb_forum_article +join tb_user on tb_forum_article.userId=tb_user.id +where tb_forum_article.cid=? and tb_forum_article.type!=2 + limit ?,?; /小频道下所有的帖子String sql2 = select tb_forum_article.id,uname username,createTime,count,tb_forum_article.title +from tb_forum_article +join tb_user on tb_forum_article.userId=tb_user.id + join tb_forum_channel on tb_forum_article.cid=

温馨提示

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

评论

0/150

提交评论