Mybaties下的分页功能的实现
jsp页面
1 <!-- 页码 --> 2 <div class="ipRListNav2"> 3 <a href="zyxx.do?findZyxx¤tPage=1">首页</a> 4 <c:forEach begin="1" end="${page.totalPage}" var="p" > 5 <c:choose> 6 <c:when test="${page.currentPage==p}"> 7 <c:if test="${p==1}"> 8 <c:if test="${page.totalPage>1}"> 9 <a href="zyxx.do?findZyxx¤tPage=${p+1}">下一页</a> 10 </c:if> 11 </c:if> 12 <c:if test="${p>1}"> 13 <c:if test="${p<page.totalPage}"> 14 <a href="zyxx.do?findZyxx¤tPage=${p-1}">上一页</a> 15 <a href="zyxx.do?findZyxx¤tPage=${p+1}">下一页</a> 16 </c:if> 17 <c:if test="${p==page.totalPage}"> 18 <a href="zyxx.do?findZyxx¤tPage=${p-1}">上一页</a> 19 </c:if> 20 </c:if> 21 </c:when> 22 </c:choose> 23 </c:forEach> 24 <a href="zyxx.do?findZyxx¤tPage=${page.totalPage}">末页</a> 25 当前 <span class="STYLE1">${page.currentPage}</span>/${page.totalPage} 页 转到 26 <input name="pageIndex" id="pageIndex" class="navIpt" type="text" onblur="if(this.value=='' || Number(this.value)!=this.value)this.value=1;" value="1" />页 27 <input name="" value="GO" class="navBtn" type="button" onclick="goPage2(document.getElementById('pageIndex').value);" /> 28 </div> 29 <script type="text/javascript"> 30 function goPage2(currentPage){ 31 alert("go"); 32 var total = ${page.totalPage}; 33 var index = currentPage.indexOf("."); 34 if(index!=-1){ 35 alert("输入数字必须为整数!请重新输入!"); 36 return; 37 } 38 if(currentPage<=0){ 39 alert("输入必须大于0!请重新输入!"); 40 return; 41 } 42 if(currentPage>total){ 43 alert("输入数字过大!请重新输入!"); 44 return; 45 } 46 47 location.href="zyxx.do?findZyxx¤tPage="+currentPage+""; 48 } 49 </script> 50
page 类
1 package org.lq.ssm.page; 2 3 public class Page { 4 5 // 分页时输入的参数 6 private int currentPage = 1; // 当前页码 7 private int pageSize = 10; // 每页显示的行数 8 9 // 写分页SQL时需要的条件,由输入参数计算而来 10 private int begin; // 当前页的起始值 11 private int end; // 当前页的终止值 12 13 14 // 用于计算总页数 15 private int rows; // 总行数 16 17 // 总页数,由rows和pageSize计算而来 18 private int totalPage; 19 20 public int getBegin() { 21 begin = (currentPage - 1) * pageSize; 22 return begin; 23 } 24 25 public int getEnd() { 26 end = currentPage * pageSize + 1; 27 return end; 28 } 29 30 public int getTotalPage() { 31 if(rows==0){ 32 totalPage = 1; 33 }else if (rows % pageSize == 0) { 34 totalPage = rows / pageSize; 35 } else { 36 totalPage = rows / pageSize + 1; 37 } 38 39 return totalPage; 40 } 41 42 public int getCurrentPage() { 43 return currentPage; 44 } 45 46 public void setCurrentPage(int currentPage) { 47 this.currentPage = currentPage; 48 } 49 50 public int getPageSize() { 51 return pageSize; 52 } 53 54 public void setPageSize(int pageSize) { 55 this.pageSize = pageSize; 56 } 57 58 public int getRows() { 59 return rows; 60 } 61 62 public void setRows(int rows) { 63 this.rows = rows; 64 } 65 66 67 }
Controller
@RequestMapping(params="findZyxx") public String findZyxx(Page page,Model m){ m.addAttribute("professionList", professionBiz.findAll(page)); page.setRows( professionBiz.count()); m.addAttribute("page",page); return "view/profession/professioninfo/professioninfo_list"; }
实现类
1 public List<Profession> findAll(Page page) { 2 3 int begin =page.getBegin(); 4 int end= page.getEnd(); 5 6 return professionDao.findPage(begin,end); 7 }
mapper文件中sql
1 <select id="findPage" resultMap="profession_map"> 2 3 select * from (select a.*,rownum r from ( select * from zyml_table ) a) where r < #{end} and r > #{begin} 4 <!-- select * from zyml_table --> 5 6 </select>
效果展示