问题
- 在普通的分页过程中,要查询出所有的数据,并查询出count才能进行分页,代码量多,处理起来比较麻烦!
处理
- 使用mybatis分页插件PageHelper,使用起来十分方便。该插件支持以下数据库:
- mysql
- oracle
- mariaDB
- SQLite
- Hsqldb
- PostgreSQL
- DB2
- SqlServer
- Informix
- H2
对应于github的项目地址:
https://github.com/pagehelper/Mybatis-PageHelper
mybatis分页插件依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.1</version>
</dependency>
插件使用的配置
- 方法一:mybatis-config.xml中配置
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 以下配置参数 -->
<property name="param1" value="value1"/>
</plugin>
</plugins>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- other configuration -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!-- 以下配置参数 -->
<value>
param1=value1
</value>
</property>
</bean>
</array>
</property>
</bean>
参考

编写mapper.xml文件

分页在service层

由于各个项目业务逻辑不同,在这就不详细展开了