mybatis的分页插件-----PageHelper

什么是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对象
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

娃娃 哈哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值