mybatis的分页操作
来源:互联网 发布:动作数据在n站 编辑:程序博客网 时间:2024/06/06 00:01
之前一直都是自己直接抄别人的分页代码,抄完能运行但是感觉不甚了解,今天我结合网上的方法重新改造了一番,觉得更加好用了,下面就来分享一下。
其实分页原理很简单,数据库分页如果不考虑条件查询的话就需要两个参数,参数一:从第几条数据考试取,参数二:取到第几条数据,有了这两个参数,我们就能做绝大部分的分页,剩下的,就是要对应页面的分页栏通过分页逻辑生成后台需要的两个参数了
那么先开始看sql server上的分页查询语句:
<!-- 根据条件查询所有用户信息进行分页 --> <select id="selectAllByExmpleWithPage" resultMap="BaseResultMap" parameterType="map"> select top ${pageRowNum} <if test="distinct" > distinct </if> <include refid="Base_Column_List" /> from user_Info where id not in (select top ${start} id from user_Info <where> <trim prefixOverrides="and"> <if test="UserName!=null"> and UserName like '%${UserName}%' </if> <if test="UserCode"> and UserCode like '%${UserCode}%' </if> </trim> </where> order by id) <trim> <if test="UserName!=null"> and UserName like '%${UserName}%' </if> <if test="UserCode"> and UserCode like '%${UserCode}%' </if> </trim> order by id </select>
接下来我们看看我的分页逻辑
有两个java类,一个是用来保存分页的数据的叫pagebean,它是用来跟页面进行信息交换的,里面定义了关于分页的参数。第二个java类是pageinfo,这个类是用来处理分页逻辑的,并把处理好的分页参数赋给pagebean。那么我们来看看这两个java类的代码:
package com.mjtrade.entity;public class PageBean { private int pageNo;//当前页码 private boolean firstPage;//是否是第一页 private boolean lastPage;//是否是最后一页 private int sumPage;//总的页数 private int pageSize;//每页展示的数量 private int totalResult;//总记录条数 public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public boolean isFirstPage() { return firstPage; } public void setFirstPage(boolean firstPage) { this.firstPage = firstPage; } public boolean isLastPage() { return lastPage; } public void setLastPage(boolean lastPage) { this.lastPage = lastPage; } public int getSumPage() { return sumPage; } public void setSumPage(int sumPage) { this.sumPage = sumPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalResult() { return totalResult; } public void setTotalResult(int totalResult) { this.totalResult = totalResult; } @Override public String toString() { return "PageBean [pageNo=" + pageNo + ", firstPage=" + firstPage + ", lastPage=" + lastPage + ", sumPage=" + sumPage + ", pageSize=" + pageSize + ", totalResult=" + totalResult + "]"; }}
另外一个处理逻辑的分类pageinfo
package com.mjtrade.entity;import javax.servlet.http.HttpServletRequest;/** * 用来计算分页的java类 * @author ljl * */public class PageInfo { //http请求 private HttpServletRequest req; //每页的记录数 private int pageSize=50; //当前页 private int currentPageNo=1; //开始记录数 private int beginResult=0; //总记录数 private int totalResult=0; //总页数 private int totalPage=0; private PageBean page=null; /** * 初始化http请求 */ public PageInfo(HttpServletRequest req){ this.currentPageNo=req.getParameter("pageNO")!=null && !req.getParameter("pageNO").equals("")?new Integer(req.getParameter("pageNO")).intValue():1; //this.pageSize = req.getParameter("pagesize")!=null && !req.getParameter("pagesize").equals("")?new Integer(req.getParameter("pagesize")).intValue():50; if(this.pageSize<=0){ this.pageSize=50; } this.req = req; } public PageInfo(){ this.currentPageNo=1; this.pageSize=50; } public PageInfo(int currentPageNo){ this.currentPageNo=currentPageNo; this.pageSize=50; } /** * 计算总页数 */ private void countPages() { if(totalResult==0) { this.totalPage=1; } else { this.totalPage = (totalResult / pageSize); //总共几页 if ((totalResult % pageSize) != 0) this.totalPage = this.totalPage + 1; } } public boolean isFirstPage(){ if(this.currentPageNo<=1){ return true; }else { return false; } } public boolean isLastPage(){ if(this.currentPageNo>=this.totalPage){ return true; }else { return false; } } /** * 获得当前页 * @return int 当前页 */ public int getCurrentPageNo() { return currentPageNo; } /** * 获得每页的记录数 * @return int */ public int getPageSize() { return pageSize; } /** * 获得总记录数 * @return int */ public int getTotalResult() { return totalResult; } /** * 设置当前页 * @param current int 当前页码 */ public void setCurrentPageNo(int current) { this.currentPageNo =current; } /** * 设置每页的记录数 * @param i int 记录数 */ public void setPageSize(int i) { this.pageSize = i; } /** * 获得开始记录数 * @return int 开始记录数 */ public int getBeginResult() { if(totalPage!=1) { if(currentPageNo>=totalPage){ currentPageNo=totalPage; beginResult= (currentPageNo - 1) * pageSize; pageSize=totalResult-beginResult; }else{ beginResult = (currentPageNo - 1) * pageSize; } } if(totalPage==1) { currentPageNo=totalPage; beginResult=0; pageSize=totalResult; } setRequestValue(); return beginResult; } public void setBeginResult(int i) { this.beginResult = i; } /** * 获得总页数 * @return int 总页数 */ public int getTotalPage() { //log.info("run here totalPage:"+totalPage); return totalPage; } public void setTotalResult(int totalResult) { this.totalResult = totalResult; countPages(); } /** * 设置序号 */ private void setRequestValue(){ page=new PageBean(); page.setFirstPage(isFirstPage()); page.setLastPage(isLastPage()); page.setPageNo(currentPageNo); page.setPageSize(pageSize); page.setSumPage(totalPage); page.setTotalResult(totalResult); } public PageBean getPageBean(){ return page; } public void setTotalPage(int totalPage){ this.totalPage=totalPage; } @Override public String toString() { return "PageInfo [req=" + req + ", pageSize=" + pageSize + ", currentPageNo=" + currentPageNo + ", beginResult=" + beginResult + ", totalResult=" + totalResult + ", totalPage=" + totalPage + ", page=" + page + "]"; }}
数据库查询,分页类,分页逻辑类都齐全了,下面就是jsp页面的分页代码了:
<form id="from1" action="${pageContext.request.contextPath }/toUserInfoList.do" method="get"> <table border="0" width="70%" cellspacing="0" cellpadding="0"> <%PageBean pagebean=(PageBean)request.getAttribute("pageBean"); %> <tr> </td> <td width="15%" align="left">总记录数:<%=pagebean.getTotalResult() %>条 </td> <td width="14%" align="right"></td> <%if(pagebean.isFirstPage()){ %> <td width="8%" align="center">首页 |</td> <td width="10%" align="center">上一页 |</td> <%}else{ %> <td width="8%" align="center"><u><a href="#" onClick="gotopage('changePage.do','start')">首页 |</a></u></td> <td width="10%" align="center"><u><a href="#" onClick="gotopage('changePage.do','prev')">上一页 |</a></u></td> <%} %> <%if(pagebean.isLastPage()){ %> <td width="10%" align="center">下一页 |</td> <td width="8%" align="center">末页</td> <%}else{ %> <td width="10%" align="center"><u><a href="#" onClick="gotopage('changePage.do','next')">下一页 |</a></u></td> <td width="8%" align="center"><u><a href="#" onClick="gotopage('changePage.do','end')">末页</a></u></td> <%} %> <td width="6%" align="center">第<%=pagebean.getPageNo() %>页 </td> <td width="6%" align="center">共<%=pagebean.getSumPage() %>页 </td> <td width="21%" align="right">至第<input size="1" type="text" name="goPage" id="goPage">页<u><a href="#" onClick="gotopage('changePage.do','go')">确定</a></u></td> <td><input id="pageNO" type="hidden" name="pageNO" value="<%=pagebean.getPageNo()%>"></td> <td><input id="prevpageNO" type="hidden" name="prevpageNO" value="<%=(pagebean.getPageNo()-1)%>"></td> <td><input id="nextpageNO" type="hidden" name="nextpageNO" value="<%=(pagebean.getPageNo()+1)%>"></td> <td><input id="sumPage" type="hidden" name="sumPage" value="<%=pagebean.getSumPage() %>"></td> <td><input id="pageSize" type="hidden" name="pageSize" value=""></td> <td><input id="initflag" type="hidden" name="initflag" value="1"> </td> </tr> </table> </form>
那分页的代码获取当前分页状态并且能够把点击的效果通过javainfo的逻辑处理赋给javabean的呢,是要通过js来处理,下面来看看页面引进js的代码:
<script type="text/javascript" src="${pageContext.request.contextPath }/js/page.js"></script><script type="text/javascript" src="${pageContext.request.contextPath }/js/validate.js"></script>
page.js:
/**path:路径where:分页处理,跳转的形式*/function gotopage(path,where){ var page=document.getElementById("pageNO").value; //当前页 if(where=="next"){ //下一页 page=document.getElementById("nextpageNO").value; }else if(where=="prev"){ //上一页 page=document.getElementById("prevpageNO").value; }else if(where=="end"){ //最后一页 page=document.getElementById("sumPage").value; }else if(where=="start"){ //首页 page=1; }else if(where=="go"){ //跳转到第几页 if(Trim(document.getElementById("goPage").value)=="") { alert("请输入页数"); document.getElementById("goPage").focus(); return false; } if(!checkNumber( document.getElementById("goPage"))) { alert("请输入正确页数(数字)"); document.getElementById("goPage").focus(); return false; } var objgo=parseInt(document.getElementById("goPage").value); var objsum=parseInt(document.getElementById("sumPage").value); if(objgo<=0||objgo>objsum){ alert("不存在此页,请重新输入页数"); document.getElementById("goPage").focus(); return false; } page=document.getElementById("goPage").value; } document.getElementById("pageNO").value=page; document.getElementById("from1").submit(); }
最后我们来看看后台是怎么通过获取javainfo的分页参数并且把两个最重要的参数传到数据库查询语句:
@RequestMapping("toUserInfoList.do") public String toUserInfoList(HttpServletRequest request){ PageInfo info=new PageInfo(request); int totalResult=0; totalResult=userInfoService.queryCountofUserInfoByExample(null); info.setTotalResult(totalResult); //设置分页参数beginResult:从第几条开始取pageSize:一共要取几条记录 //[输入参数]:int start:开始,int pageRowNum:每页数据量,Map<String, String> maps查询条件 int beginResult=info.getBeginResult(); int pageSize=info.getPageSize(); Map<String, Object> maps=new HashMap<String, Object>(); maps.put("start", beginResult); maps.put("pageRowNum", pageSize); maps.put("UserName", "1"); //从数据库中取出相应数据 List<UserInfo> infos= userInfoService.queryAllUserInfoWithPage(maps); request.setAttribute("infos", infos); PageBean bean=info.getPageBean(); request.setAttribute("pageBean", bean); return "forward:/page1.jsp"; }
其实分页一点也不难,只要理解好里面的关系就可以了
0 0
- mybatis的分页操作
- MyBatis的分页操作(MySQL)
- MyBatis的分页操作(MySQL)
- mybatis 简单分页操作
- mybatis的分页机制
- MyBatis的分页
- mybatis的分页插件
- mybatis 的分页
- mybatis分页的实现
- MyBatis 分页的实现
- SpringMVC的mybatis分页
- Mybatis 分页的实现
- Mybatis的分页插件
- Mybatis的分页插件
- 使用mybatis对SQLite数据库分页操作
- Mybatis操作Sybase数据库游标分页
- MyBatis增删改查操作、MyBatis存储过程、MyBatis分页、MyBatis一对一、MyBatis一对多
- mybatis的物理分页:mybatis-paginator
- 连接SQL*Plus Worksheet
- HDU1108——最小公倍数
- POJ 3281 Dining 题解
- Android自定义ViewGroup添加组合控件的子view
- Android TV Input Framework(TIF)--3 显示Tv Input内容
- mybatis的分页操作
- Session与Cookie
- [SVN] 配置本地svn非常简洁的说明书
- Android Studio使用细节总结
- BeanFactory和FactoryBean的介绍
- java.util.ConcurrentModificationException异常解决
- leetcode之number of 1 bits
- UVALive 3708_Graveyard
- CSS代码重构与优化之路