一. 在pom.xml导入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
二. 交给IOC管理 在SqlSessionFactory
传入插入对象
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注意其他配置 -->
<!-- 传入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>
解释:
- helperDialect : 用的什么数据库
- reasonable : 可以
true
或者false
使用true 当页数是1向上翻页的时候,会一直保持1, 最后一页也是同理
使用false 当页数是1向上翻页的时候,会变成0,继续点会变成负数...
三.在执行sql的前面,使用pageHelper来完成分页
PageHelper.startPage(1,5)
这里是sql语句 findAll() 上面是分页操作
1是页码值,5是每页显示条数