什么是mybatis的分页插件
PageHelper是一款国内开源免费的Mybatis第三方物理分页插件
配置分页插件
引入依赖
在pom.xmlr中导入依赖
<!--国内开发分页助手,基于mybatis开发的-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.4</version>
</dependency>
<!--
pageHelper 5.1.5 分页拦截器是 PageInterceptor, 返回的是PageInfo
pageHelper 4.1.4:分页拦截器是 PageHelper, 返回是Page对象
-->
配置方法一
在 SqlSessionFactory中配置分页插件
<property name="plugins" >
<!--注入数组对象-->
<array>
<!--注入: Interceptor类型的对象-->
<bean class="com.github.pagehelper.PageHelper">
<!--指定方言-->
<property name="properties">
<props>
<!--指定方言为mysql数据库-->
<prop key="dialect">mysql</prop>
</props>
</property>
</bean>
</array>
</property>
4.2.3 配置方法二
在SqlMapConfig(mybatis-config).xml中配置分页插件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置插件 -->
<plugins>
<!--引入分页拦截器(插件): 可以自动选择数据库方言-->
<plugin interceptor="com.github.pagehelper.PageHelper"></plugin>
</plugins>
</configuration>
在SqlSessionFactory中引入SqlMapConfig
<!--引入mybatis配置文件: 包括分页插件-->
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
在service层使用分页功能时,先调用startPage
方法开启分页功能 然后调用dao中的查询方法 会返回一个Page对象 (4.1.1版本)
@Override
public PageResult findByPage(QueryPageBean queryPageBean) {
PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
Page<CheckItem> page = checkItemDao.findByCondition(queryPageBean.getQueryString());
return new PageResult(page.getTotal(), page);
}
调用的方法为
/**
* 开始分页
*
* @param pageNum 页码
* @param pageSize 每页显示数量
*/
public static <E> Page<E> startPage(int pageNum, int pageSize) {
return startPage(pageNum, pageSize, true);
}
返回的page对象中的数据为
page = Page{count=true, pageNum=1, pageSize=10, startRow=0, endRow=10, total=65, pages=7, countSignal=false, orderBy='null', orderByOnly=false, reasonable=false, pageSizeZero=false}
其实返回的page是一个集合对象,继承了ArrayList
/**
* Mybatis - 分页对象
*
* @author liuzh/abel533/isea533
* @version 3.6.0
* 项目地址 : http://git.oschina.net/free/Mybatis_PageHelper
*/
public class Page<E> extends ArrayList<E> {
private static final long serialVersionUID = 1L;
其实page对象中存储的就是查询得到的数据
例如 遍历page对象 得到的就是查询数据的的对象
for (CheckItem checkItem : page) {
System.out.println("checkItem = " + checkItem);
}
return new PageResult(page.getTotal(), page);
/*checkItem = com.itheima.pojo.CheckItem @ec3d676
checkItem = com.itheima.pojo.CheckItem @ 70f 66 a0a
checkItem = com.itheima.pojo.CheckItem @ 27623060
checkItem = com.itheima.pojo.CheckItem @ 55907d dd
checkItem = com.itheima.pojo.CheckItem @ 45f 0717f
checkItem = com.itheima.pojo.CheckItem @ 695f 5f 1
checkItem = com.itheima.pojo.CheckItem @ 30422473
checkItem = com.itheima.pojo.CheckItem @ 666 ac933
checkItem = com.itheima.pojo.CheckItem @ 20573708
checkItem = com.itheima.pojo.CheckItem @ 9 c4748e*/
原理分析
调用PageHelper.startPage 方法, 会自动把 currentPage,pageSize两个参数自动存入到 ThreadLocal对象中。
进入setLocalPage方法
PageHelper 本身实现了拦截器接口,在调用了PageHelper.startPage() 方法的下一次查询时,会拦截查询操作。
拦截查询请求后,会自动从线程中获取Page对象, 根据mybatis映射文件中配置的sql语句和page对象,进行sql拼接,最终执行sql,返回Page对象