1.1介绍
- PageHelper是国内非常优秀的一款开源的mybatis分页插件, 它支持基本主流与常用的数据库, 例如mysql、 oracle、 mariaDB、 DB2、 SQLite、 Hsqldb等。
- 本项目在 github 的项目地址: https://github.com/pagehelper/Mybatis-PageHelper
- 本项目在 gitosc 的项目地址: http://git.oschina.net/free/Mybatis_PageHelper
- 进入https://pagehelper.github.io/网址, 点击左上角文档进行PageHelper的使用学习
1.2 依赖
在pom.xml中添加如下依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.4</version></dependency>
1.3 如何使用PageHelper
- 特别注意,新版拦截器是com.github.pagehelper.PageInterceptor.
- com.github.pagehelper.PageHelper现在是一个特殊的dialect实现类,是分页插件的默认实现类。
1.3.1 在mybatis的xml中配置拦截器插件
- mybatis如果是使用配置的方式:
<plugins>
</plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数, 后面会有所有的参数介绍 -->
<property name="param1" value="value1"/>
</plugin>
- 注意:plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
顺序 | 配置标签名称 | 说明 |
1 | properties | 属性 |
2 | settings | 配置全局参数 |
3 | typeAliases | 类型别名 |
4 | typeHandlers | 类型处理器 |
5 | objectFactory | 对象工厂 |
6 | objectWrapperFactory | |
7 | plugins | 插件 |
8 | environments | 环境集合属性对象 |
9 | databaseIdProvider | 多数据库支持 |
10 | mappers | 映射器 |
说明:
|
1.3.2 Spring整合mybatis配置文件中配置拦截器插件
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
</bean>
<!-- 注意其他配置 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置参数, 一行配置一个 -->
<value>
params=value1
</value>
</property>
</bean>
</array>
</property>
1.3.3 参数介绍
- 常用的属性为helperDialect与reasonable:
1. helperDialect : 分页插件会自动检测当前的数据库连接, 自动选择合适的分页方式。 你可以配置 helperDialect 属性来指定分页插件使用哪种方言。 配置时, 可以使用下面的缩写值:oracle , mysql , mariadb , sqlite , hsqldb , postgresql , db2 , sqlserver , informix , h2, sqlserver2012 , derby 特别注意: 使用 SqlServer2012 数据库时, 需要手动指定为sqlserver2012 , 否则会使用 SqlServer2005 的方式进行分页。 你也可以实现AbstractHelperDialect , 然后配置该属性为实现类的全限定名称即可使用自定义的实现方法。
2. offsetAsPageNum : 默认值为 false , 该参数对使用 RowBounds 作为分页参数时有效。 当该参数设置为 true 时, 会将 RowBounds 中的 offset 参数当成 pageNum 使用, 可以用页码和页面大小两个参数进行分页。
3. rowBoundsWithCount : 默认值为 false , 该参数对使用 RowBounds 作为分页参数时有效。当该参数设置为 true 时, 使用 RowBounds 分页会进行 count 查询。
4. pageSizeZero : 默认值为 false , 当该参数设置为 true 时, 如果 pageSize=0 或者RowBounds.limit = 0 就会查询出全部的结果( 相当于没有执行分页查询, 但是返回结果仍然是 Page 类型) 。
5. reasonable : 分页合理化参数, 默认值为 false 。 当该参数设置为 true 时, pageNum<=0时会查询第一页, pageNum>pages ( 超过总数时) , 会查询最后一页。 默认 false 时, 直接根据参数进行查询。
6. params : 为了支持 startPage(Object params) 方法, 增加了该参数来配置参数映射, 用于从对象中根据属性名取值, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable , 不配置映射的用默认值, 默认值
为 pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero 。
7. supportMethodsArguments : 支持通过 Mapper 接口参数来传递分页参数, 默认值 false , 分页插件会从查询方法的参数值中, 自动根据上面 params 配置的字段中取值, 查找到合适的值时就会自动分页。 使用方法可以参考测试代码中的 com.github.pagehelper.test.basic 包下的 ArgumentsMapTest 和 ArgumentsObjTest 。
8. autoRuntimeDialect : 默认值为 false 。 设置为 true 时, 允许在运行时根据多数据源自动识别对应方言的分页 ( 不支持自动选择 sqlserver2012 , 只能使用 sqlserver ) , 用法和注意事项参考下面的场景五。
9. closeConn : 默认值为 true 。 当使用运行时动态数据源或没有设置 helperDialect 属性自动获取数据库类型时, 会自动获取一个数据库连接, 通过该属性来设置是否关闭获取的这个连接, 默认 true 关闭, 设置为 false 后, 不会关闭获取的连接, 这个参数的设置要根据自己选择的数据源来决定。
1.3.4 PageHelper的基本用法
PageHelper提供了很多分页的方式,详细的可以在官网中查看,常见的如下:
- 在你需要进行分页的mybatis查询方法前调用调用PageHelper.startPage静态方法即可,紧跟在这个方法后的第一个mybatis查询方法会被进行分页。
//获取第1页, 10条内容, 默认查询总数count
PageHelper.startPage(1, 10);
//紧跟着的第一个select方法会被分页
List<Country> list = countryMapper.selectAll();
2.1 项目中使用PageHelper
2.1.1 第一步:添加依赖
2.1.2 第二步:在applicationContext.xml中配置拦截器插件
<!--3. 创建SqlSessionFactoryBean,注入连接池-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--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>
- 完整applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--1.扫描service包-->
<context:component-scan base-package="com.sunny.service"></context:component-scan>
<!--加载jdbc.properties配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--2. 创建连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="initialSize" value="${jdbc.initialSize}"></property>
<property name="maxActive" value="${jdbc.maxActive}"></property>
</bean>
<!--3. 创建SqlSessionFactoryBean,注入连接池-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--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>
<!--4. 创建包扫描器,指定扫描的dao接口所在包-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sunny.dao"></property>
</bean>
<!--5. Spring提供的事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--6. 开启声明式事务注解支持-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--7. 加载其他的配置文件-->
<import resource="classpath:spring-security.xml"/>
</beans>