这是继上一个的基于JavaWeb的又一个不同的分页代码,直接分离查询,可靠而实用,上一个分页代码比较简单,原理是直接获取数据库的所有数据,这样实现的分页虽然很快,毕竟查出来的数据直接保存在集合中,随后的分页只是实现了取得其中的一部分数据而已,但弊端是如果数据量大的话,可能就会造成内存溢出的现象,所以鉴于此,我这次把这个直接采用数据库中分页原理把代码贴出来,希望对于很多朋友都有所帮助,并且有分页API,希望对大家有所帮助。
在这里我把使用步骤给大家说一下(呵呵~我在网上也找了很多的分页代码,我不知道是很难还是自己没看懂,就没怎么一个很完整的代码,在这里我只是共享我的一些学习心得,鉴于此,我把详细的使用步骤列在下面)
分页使用步骤使用:(此使用步骤是基于struts的,类似servlet的,大家可以模仿此调用写出即可)
一、将以下Paging类加入到你的项目中
二、在action中加入以下两个属性
1.//分页(必需) 2.private Paging paging; 3. 4.//当前页码(必需) 5.private int cur_page; 6. 7.public int getCur_page() { 8.return cur_page; 9.} 10. 11.public void setCur_page(int cur_page) { 12.this.cur_page = cur_page; 13.}
三、调用
1.//(必需) 2.this.paging = new Paging(); 3. //(必需) 4.if(this.cur_page == 0) { 5.this.cur_page = 1; 6.} 7. 8.//设置当前页码(必需) 9.this.paging.setCurPage(this.cur_page); 10. 11.//把得到总共显示的数据记录设置在分页总纪录中(必需) 12.this.paging.setRowCount(userMagr.count()); 13. 14.//设置每页显示的数据记录(必需) 15.this.paging.setPageRowCount(3); 16. 17.//保存分页工具条(必需) 18.request.setAttribute("toolBar", this.paging.getToolBar("user_list.do")); 19. 20.//得到每页显示数据(必需) 21.this.users = userMagr.list(paging);
分页源代码:
1.package net.etwo.util; 2. 3.public class Paging { 4. 5./** 6.* 分页 7.* @说明:本分页采用直接获取每一页的数据 8.* @author Etwo 9.*/ 10. 11./* 12.* 分页要素: 13.* 1、首页 first 14.* 2、最后一页 last 15.* 3、页总数 pageCount 16.* 4、总共显示的数据记录 rowCount 17.* 5、每页显示的数据记录 pageRowCount 18.* 6、当前页 curPage 19.* 7、定义每一页的开始 start 20.* 8、定义每一页的结束 end 21.*/ 22. 23./** 24.* 1、首页 first 25.*/ 26.private int first; 27. 28./** 29.* 2、最后一页 last 30.*/ 31.private int last; 32. 33./** 34.* 3、页总数 pageCount 35.*/ 36.private int pageCount; 37. 38./** 39.* 4、总共显示的数据记录 rowCount 40.*/ 41.private long rowCount; 42. 43./** 44.* 5、每页显示的数据记录 pageRowCount 45.*/ 46.private int pageRowCount; 47. 48./** 49.* 6、当前页 curPage 50.*/ 51.private int curPage; 52. 53./** 54.* 7、定义每一页的开始 start 55.*/ 56.private int start; 57. 58./** 59.* 8、定义每一页的结束 end 60.*/ 61.private int end; 62. 63. 64./** 65.* 计算并得出页总数 66.* @return 67.*/ 68.public int getPageCount() { 69.this.pageCount = (int)Math.ceil((double)this.getRowCount() / (double)this.getPageRowCount()); 70.return pageCount; 71.} 72. 73./** 74.* 得到总共显示的数据记录 75.* @return 76.*/ 77.public long getRowCount() { 78.return rowCount; 79.} 80. 81./** 82.* 设置总共显示的数据记录 83.* @param rowCount 84.*/ 85.public void setRowCount(long rowCount) { 86.this.rowCount = rowCount; 87.} 88. 89./** 90.* 得到每页显示的数据记录 91.* @return 92.*/ 93.public int getPageRowCount() { 94.return pageRowCount; 95.} 96. 97./** 98.* 设置每页显示的数据记录 99.* @param pageRowCount 100.*/ 101.public void setPageRowCount(int pageRowCount) { 102.this.pageRowCount = pageRowCount; 103.} 104. 105./** 106.* 得到当前页 107.* @return 108.*/ 109.public int getCurPage() { 110.return curPage; 111.} 112. 113./** 114.* 设置当前页 115.* @param curPage 116.*/ 117.public void setCurPage(int curPage) { 118.this.curPage = curPage; 119.} 120. 121./** 122.* 得到每页的开始记录数 123.* @return 124.*/ 125.public int getStart() { 126.this.start = (this.getCurPage() - 1) * this.getPageRowCount(); 127.return start; 128.} 129. 130./** 131.* 得到每页的结束记录数 132.* @return 133.*/ 134.public int getEnd() { 135.this.end = this.getPageRowCount(); 136.return end; 137.} 138. 139./** 140.* 得到首页 141.* @return 142.*/ 143.public int getFirst() { 144.return this.first = 1; 145.} 146. 147./** 148.* 得到最后一页 149.* @return 150.*/ 151.public int getLast() { 152.this.last = (int)this.getPageCount(); 153.return last; 154.} 155. 156./** 157.* 上一页 158.* @return 159.*/ 160.public int previous() { 161.return (this.getCurPage() > 1) ? this.getCurPage() - 1 : 1; 162.} 163. 164./** 165.* 下一页 166.* @return 167.*/ 168.public int next() { 169.return (this.getCurPage() < (int)this.getPageCount()) ? this.getCurPage() + 1 : (int)this.getPageCount(); 170.} 171. 172./** 173.* 判断是否是首页 174.* @return 175.*/ 176.public boolean isFirst() { 177.return (this.getCurPage() == 1) ? true : false; 178.} 179. 180./** 181.* 判断是否是最后一页 182.* @return 183.*/ 184.public boolean isLast() { 185.return (this.getCurPage() == this.getPageCount()) ? true : false; 186.} 187. 188./** 189.* 客户端显示的工具条 190.*/ 191.public String getToolBar(String url) { 192. String str, temp; 193. 194. //用于判断url中是否存在? 195. if(url.indexOf("?") == -1) { 196. temp = "?"; 197. } else { 198. temp = "&"; 199. } 200. 201. str = "<form method='post' name='frmPage' action='" + url + "'> "; 202. str += "<p align='right' id='page'>"; 203. str += "<span style='font-size: 12px; font-weight: bold;'>"; 204. //判断是否是首页 205. if(isFirst()) { 206. str += "首页 上一页 "; 207. } else { 208. str += "<a href='" + url + temp + "cur_page=" + this.getFirst() + "'>首页</a> "; 209. str += "<a href='" + url + temp + "cur_page=" + this.previous() + "'>上一页</a> "; 210. } 211. //判断是否是最后一页 212. if(isLast()) { 213. str += "下一页 尾页 "; 214. } else { 215. str += "<a href='" + url + temp + "cur_page=" + this.next() + "'>下一页</a> "; 216. str += "<a href='" + url + temp + "cur_page=" + this.getLast() + "'>尾页</a> "; 217. } 218. str += " 共<b>" + this.rowCount + "</b>条记录 "; 219. str += " 转到<select name='page' onChange=\"location='" + url + temp + "cur_page='+this.options[this.selectedIndex].value\">"; 220. 221. for(int i = 1; i <= this.getPageCount(); i++) { 222. //判断是否是当前页,若是,则默认为选中当前页 223. if(i == curPage) 224. str += "<option value='" + i + "' selected>第" + i + "页</option>"; 225. else 226. str += "<option value='" + i + "'>第" + i + "页</option>"; 227. } 228. str += "</select></span></p></form>"; 229. return str; 230.} 231.}