Mybatis分页插件PageHelper的学习与使用
目录
后端程序员都知道,在web系统中,分页是一种常见的功能,我之前写的分页方法都比较麻烦,移植性也不高,这就很不乐观了。作为一个积极开朗的程序员,怎么能不去了解pagehelper分页插件呢?pagehelper是国内非常优秀的一款开源的mybatis分页插件,它支持基本主流与常用的数据库,一致支持mysql、oracle、mariadb、db2、sqlite、hsqldb等。ok接下来就一起来了解了解pagehelper分页插件!
@
中文教程
首先,推荐两个pagehelper分页插件项目地址,这里面有中文教程:
github 项目
gitosc 项目
pagehelper使用
至于怎么使用我想上面提供的两个项目讲的绝逼讲的比我清楚,下面直接进入pagehelper使用实战
首先,集成引入分页插件有下面2种方式,一种是直接下jar包一种是mawen依赖,推荐使用 maven 方式。
方法一: 直接下载jar包
,由于使用了sql 解析工具,你还需要下载
方法二: maven添加依赖
<dependency> <groupid>com.github.pagehelper</groupid> <artifactid>pagehelper</artifactid> <version>5.1.2</version> </dependency>
1.导入maven依赖分页插件
我这里就采用方法二maven依赖
2.各个情况下xml的配置
情况一
:如果是mybatis单独使用pagehelper分页插件的时候,就需要在xml配置中添加如下代码:
<!-- plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下properties?, settings?, typealiases?, typehandlers?, objectfactory?,objectwrapperfactory?, plugins?, environments?, databaseidprovider?, mappers? --> <!-- com.github.pagehelper为pagehelper类所在包名 --> <plugins> <!-- 使用下面的方式配置参数,推荐的两个项目中有所有的参数介绍 --> <plugin interceptor="com.github.pagehelper.pageinterceptor"> <property name="param1" value="value1"/> </plugin> </plugins>
情况二
:显然,我是这个是ssm项目,mybatis是交由spring的ioc容器管理的,就需要在spring的xml配置中添加如下代码(创建工厂中添加):
<!-- 把交给ioc管理 sqlsessionfactory --> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource"/> <!-- 传入pagehelper的插件 --> <property name="plugins"> <array> <!-- 传入插件的对象 --> <bean class="com.github.pagehelper.pageinterceptor"> <property name="properties"> <props> <prop key="helperdialect">oracle</prop> <prop key="reasonable">true</prop> </props> </property> </bean> </array> </property> </bean>
下面用一个图来分析一下两个参数作用:
更多情况请参考上面推荐的项目~
3.controller代码编写
上面我们配置完了,至于为什么从controller代码开始是因为controller调用service嘛,这样编写代码更方便快捷!为了看出差别,未分页的controller代码方法如下:
@controller @requestmapping("/orders") public class orderscontroller { @autowired private iordersservice ordersservice; //查询所有orders未分页 @requestmapping("/findall.do") public modelandview findall() { modelandview mv = new modelandview(); list<orders> orderslist = ordersservice.findall(); mv.addobject("orderslist", orderslist); mv.setviewname("orders-list"); return mv; } }
而采用了分页代码则是如下:
//采用分页代码方法 @requestmapping("/findall.do") public modelandview findall(@requestparam(name="page",required = true,defaultvalue = "1")int page,@requestparam(name="size",required = true,defaultvalue = "4")int size ) { modelandview mv = new modelandview(); list<orders> orderslist = ordersservice.findall(page,size); //pageinfo就是一个分页bean pageinfo pageinfo =new pageinfo(orderslist); mv.addobject("pageinfo", pageinfo); mv.setviewname("orders-list"); return mv; }
可以发现多了两个参数分别是page
和size
4.service接口代码编写
public interface iordersservice { list<orders> findall(int page,int size); }
5.service实现类代码编写
在执行sql之前(serviceimpl),使用pagehelper分页,参数pagenum
是页码值 参数pagesize
代表是每页显示条数。在serviceimpl中,使用pagehelper分页操作代码如下:
@service public class ordersserviceimpl implements iordersservice { @autowired private iordersdao ordersdao; @override public list<orders> findall(int page,int size) { //参数pagenum 是页码值 参数pagesize 代表是每页显示条数 pagehelper.startpage(page, size); return ordersdao.findall(); } }
特别注意:在编写pagehelper分页代码时的 pagehelper.startpage(page, size)
这一句必须写在查询方法的上一句,中间不能添加任何代码,否则失效!
6.jsp页面未处理代码
没有更改的jsp页面代码,普通查询关键更改代码块,注意这里接收数据为 orderslist
<c:foreach items="${orderslist}" var="orders"> <tr> <td><input name="ids" type="checkbox"></td> ... <td>...</td> </td> </tr> </c:foreach>
这下面的代码则是未分页逻辑代码
<div class="box-tools pull-right"> <ul class="pagination"> <li> <a href="#" aria-label="previous">首页</a> </li> <li><a href="#">上一页</a></li> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">下一页</a></li> <li> <a href="#" aria-label="next">尾页</a> </li> </ul> </div> </div>
7.jsp页面处理后代码
更改后的jsp页面代码,分页查询的关键更改代码块,注意这里接收数据为 orderslist.list
<c:foreach items="${orderslist.list}" var="orders"> <tr> <td><input name="ids" type="checkbox"></td> ... <td>...</td> </td> </tr> </c:foreach>
这下面的代码则是分页逻辑代码
<div class="box-tools pull-right"> <ul class="pagination"> <li> <a href="${pagecontext.request.contextpath}/orders/findall.do?page=1&size=${pageinfo.pagesize}" aria-label="previous">首页</a> </li> <li><a href="${pagecontext.request.contextpath}/orders/findall.do?page=${pageinfo.pagenum-1}&size=${pageinfo.pagesize}">上一页</a></li> <c:foreach begin="1" end="${pageinfo.pages}" var="pagenum"> <li><a href="${pagecontext.request.contextpath}/orders/findall.do?page=${pagenum}&size=${pageinfo.pagesize}">${pagenum}</a></li> </c:foreach> <li><a href="${pagecontext.request.contextpath}/orders/findall.do?page=${pageinfo.pagenum+1}&size=${pageinfo.pagesize}">下一页</a></li> <li> <a href="${pagecontext.request.contextpath}/orders/findall.do?page=${pageinfo.pages}&size=${pageinfo.pagesize}" aria-label="next">尾页</a> </li> </ul> </div> </div>
8.jsp页面处理前后代码分析
//pageinfo就是一个分页bean pageinfo pageinfo =new pageinfo(orderslist);
为了能看到上面的pageinfo源代码,我就把代码贴在下面了
package com.github.pagehelper; import java.io.serializable; import java.util.collection; import java.util.list; public class pageinfo<t> implements serializable { private static final long serialversionuid = 1l; private int pagenum; private int pagesize; private int size; private int startrow; private int endrow; private long total; private int pages; private list<t> list; private int prepage; private int nextpage; private boolean isfirstpage; private boolean islastpage; private boolean haspreviouspage; private boolean hasnextpage; private int navigatepages; private int[] navigatepagenums; private int navigatefirstpage; private int navigatelastpage; public pageinfo() { this.isfirstpage = false; this.islastpage = false; this.haspreviouspage = false; this.hasnextpage = false; } public pageinfo(list<t> list) { this(list, 8); } public pageinfo(list<t> list, int navigatepages) { this.isfirstpage = false; this.islastpage = false; this.haspreviouspage = false; this.hasnextpage = false; if (list instanceof page) { page page = (page)list; this.pagenum = page.getpagenum(); this.pagesize = page.getpagesize(); this.pages = page.getpages(); this.list = page; this.size = page.size(); this.total = page.gettotal(); if (this.size == 0) { this.startrow = 0; this.endrow = 0; } else { this.startrow = page.getstartrow() + 1; this.endrow = this.startrow - 1 + this.size; } } else if (list instanceof collection) { this.pagenum = 1; this.pagesize = list.size(); this.pages = this.pagesize > 0 ? 1 : 0; this.list = list; this.size = list.size(); this.total = (long)list.size(); this.startrow = 0; this.endrow = list.size() > 0 ? list.size() - 1 : 0; } if (list instanceof collection) { this.navigatepages = navigatepages; this.calcnavigatepagenums(); this.calcpage(); this.judgepageboudary(); } } private void calcnavigatepagenums() { int i; if (this.pages <= this.navigatepages) { this.navigatepagenums = new int[this.pages]; for(i = 0; i < this.pages; ++i) { this.navigatepagenums[i] = i + 1; } } else { this.navigatepagenums = new int[this.navigatepages]; i = this.pagenum - this.navigatepages / 2; int endnum = this.pagenum + this.navigatepages / 2; int i; if (i < 1) { i = 1; for(i = 0; i < this.navigatepages; ++i) { this.navigatepagenums[i] = i++; } } else if (endnum > this.pages) { endnum = this.pages; for(i = this.navigatepages - 1; i >= 0; --i) { this.navigatepagenums[i] = endnum--; } } else { for(i = 0; i < this.navigatepages; ++i) { this.navigatepagenums[i] = i++; } } } } private void calcpage() { if (this.navigatepagenums != null && this.navigatepagenums.length > 0) { this.navigatefirstpage = this.navigatepagenums[0]; this.navigatelastpage = this.navigatepagenums[this.navigatepagenums.length - 1]; if (this.pagenum > 1) { this.prepage = this.pagenum - 1; } if (this.pagenum < this.pages) { this.nextpage = this.pagenum + 1; } } } private void judgepageboudary() { this.isfirstpage = this.pagenum == 1; this.islastpage = this.pagenum == this.pages || this.pages == 0; this.haspreviouspage = this.pagenum > 1; this.hasnextpage = this.pagenum < this.pages; } public int getpagenum() { return this.pagenum; } public void setpagenum(int pagenum) { this.pagenum = pagenum; } public int getpagesize() { return this.pagesize; } public void setpagesize(int pagesize) { this.pagesize = pagesize; } public int getsize() { return this.size; } public void setsize(int size) { this.size = size; } public int getstartrow() { return this.startrow; } public void setstartrow(int startrow) { this.startrow = startrow; } public int getendrow() { return this.endrow; } public void setendrow(int endrow) { this.endrow = endrow; } public long gettotal() { return this.total; } public void settotal(long total) { this.total = total; } public int getpages() { return this.pages; } public void setpages(int pages) { this.pages = pages; } public list<t> getlist() { return this.list; } public void setlist(list<t> list) { this.list = list; } /** @deprecated */ @deprecated public int getfirstpage() { return this.navigatefirstpage; } /** @deprecated */ @deprecated public void setfirstpage(int firstpage) { this.navigatefirstpage = firstpage; } public int getprepage() { return this.prepage; } public void setprepage(int prepage) { this.prepage = prepage; } public int getnextpage() { return this.nextpage; } public void setnextpage(int nextpage) { this.nextpage = nextpage; } /** @deprecated */ @deprecated public int getlastpage() { return this.navigatelastpage; } /** @deprecated */ @deprecated public void setlastpage(int lastpage) { this.navigatelastpage = lastpage; } public boolean isisfirstpage() { return this.isfirstpage; } public void setisfirstpage(boolean isfirstpage) { this.isfirstpage = isfirstpage; } public boolean isislastpage() { return this.islastpage; } public void setislastpage(boolean islastpage) { this.islastpage = islastpage; } public boolean ishaspreviouspage() { return this.haspreviouspage; } public void sethaspreviouspage(boolean haspreviouspage) { this.haspreviouspage = haspreviouspage; } public boolean ishasnextpage() { return this.hasnextpage; } public void sethasnextpage(boolean hasnextpage) { this.hasnextpage = hasnextpage; } public int getnavigatepages() { return this.navigatepages; } public void setnavigatepages(int navigatepages) { this.navigatepages = navigatepages; } public int[] getnavigatepagenums() { return this.navigatepagenums; } public void setnavigatepagenums(int[] navigatepagenums) { this.navigatepagenums = navigatepagenums; } public int getnavigatefirstpage() { return this.navigatefirstpage; } public int getnavigatelastpage() { return this.navigatelastpage; } public void setnavigatefirstpage(int navigatefirstpage) { this.navigatefirstpage = navigatefirstpage; } public void setnavigatelastpage(int navigatelastpage) { this.navigatelastpage = navigatelastpage; } public string tostring() { stringbuffer sb = new stringbuffer("pageinfo{"); sb.append("pagenum=").append(this.pagenum); sb.append(", pagesize=").append(this.pagesize); sb.append(", size=").append(this.size); sb.append(", startrow=").append(this.startrow); sb.append(", endrow=").append(this.endrow); sb.append(", total=").append(this.total); sb.append(", pages=").append(this.pages); sb.append(", list=").append(this.list); sb.append(", prepage=").append(this.prepage); sb.append(", nextpage=").append(this.nextpage); sb.append(", isfirstpage=").append(this.isfirstpage); sb.append(", islastpage=").append(this.islastpage); sb.append(", haspreviouspage=").append(this.haspreviouspage); sb.append(", hasnextpage=").append(this.hasnextpage); sb.append(", navigatepages=").append(this.navigatepages); sb.append(", navigatefirstpage=").append(this.navigatefirstpage); sb.append(", navigatelastpage=").append(this.navigatelastpage); sb.append(", navigatepagenums="); if (this.navigatepagenums == null) { sb.append("null"); } else { sb.append('['); for(int i = 0; i < this.navigatepagenums.length; ++i) { sb.append(i == 0 ? "" : ", ").append(this.navigatepagenums[i]); } sb.append(']'); } sb.append('}'); return sb.tostring(); } }
到这里,基本就ok了,我再次强调我推荐的那两个项目,那里面讲的很详细大佬写的,强调完毕,挥爪~
推荐阅读
-
java使用插件pagehelper在mybatis中实现分页查询
-
MyBatis 分页插件 PageHelper 使用
-
bootstrap table插件的分页与checkbox使用详解
-
Springboot集成mybatis通用Mapper与分页插件PageHelper
-
springboot 整合 Mybatis的分页插件 PageHelper
-
Springboot整合Mybatis使用分页 PageHelper分页插件
-
Spring boot整合Mybatis-plus分页插件的使用
-
使用mybatis分页插件PageHelper5.1.2遇到的问题
-
使用mybatis分页插件PageHelper5.0.0遇到的问题总结
-
mybatis中分页插件的使用:(基于SSM+Maven+jsp)