mybatis分页插件—PageHelper插件
数据库分页:物理分页和内存分页
-
内存分页:先将所有数据加载内存中,然后从内存中查找分页的数据
-
物理分页:在数据检索数据的时候,只检索分页数据,将数据返回给客户端。
MyBatis提供内存分页 RowBounds参数
Mybatis物理分页
-
自己写sql语句 sql limit ?,?
-
MyBatis插件 MyBatis PageHelper
PageHelper的使用步骤
-
导人jar
jsqlparser-2.1.jar
pagehelper-5.1.9.jar -
spring集成
spring-datasource.xml加入分页插件代码
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
<value>
helperDialect=mysql
</value>
</property>
</bean>
</array>
</property>
第3步:在service层进行调用分页的方法
PageHelper.startPage(1,5);
List<CstCustomer> cstCustomers = cstCustomerMapper.selectByExample(null);
查询第1页 每页显示5条
只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询(Select)方法会被分页。