Mybatis的通用分页插件,简化分页查询代码:
分页插件-Mybatis-PageHelper详解篇https://www.jianshu.com/p/637254b99835
GitHub源码地址: https://github.com/pagehelper/Mybatis-PageHelperhttps://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. 配置拦截器插件
(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>