zl程序教程

您现在的位置是:首页 >  大数据

当前栏目

分页插件-Mybatis-PageHelper配置

配置mybatis插件 分页 pagehelper
2023-09-14 09:14:08 时间

Mybatis的通用分页插件,简化分页查询代码:

      分页插件-Mybatis-PageHelper详解篇icon-default.png?t=M276https://www.jianshu.com/p/637254b99835
GitHub源码地址: https://github.com/pagehelper/Mybatis-PageHelpericon-default.png?t=M276https://links.jianshu.com/go?to=https%3A%2F%2Fgithub.com%2Fpagehelper%2FMybatis-PageHelper

 

1. 使用 Maven

(1)在 pom.xml 中添加如下依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>最新版本</version>
</dependency>

2. 配置拦截器插件

特别注意,新版拦截器是 com.github.pagehelper.PageInterceptorcom.github.pagehelper.PageHelper 现在是一个特殊的 dialect 实现类,是分页插件的默认实现类,提供了和以前相同的用法

(1)在 MyBatis 配置 xml 中配置拦截器插件

<!-- 
    plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
    properties?, settings?, 
    typeAliases?, typeHandlers?, 
    objectFactory?,objectWrapperFactory?, 
    plugins?, 
    environments?, databaseIdProvider?, mappers?
-->
<plugins>
    <!-- com.github.pagehelper为PageHelper类所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
        <property name="param1" value="value1"/>
    </plugin>
</plugins>

(2)在 Spring 配置文件中配置拦截器插件

使用 spring 的属性配置方式,可以使用 plugins 属性像下面这样配置:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <!-- 注意其他配置 -->
  <property name="plugins">
    <array>
      <bean class="com.github.pagehelper.PageInterceptor">
        <property name="properties">
          <!--使用下面的方式配置参数,一行配置一个 -->
          <value>
            params=value1
          </value>
        </property>
      </bean>
    </array>
  </property>
</bean>