分页查询
pagehelper
(官网地址:https://pagehelper.github.io/)
特点:1)支持多种数据库 2)支持多种分页方式 3)
pagehelper的使用
1)引入依赖(引入分页插件)
1、springboot集成
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
2、spring集成
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>最新版本</version>
</dependency>
这里引入的是整合的springboot的jar包
2)配置分页插件(三种方式)
(也称为配置拦截器插件)
参数配置(参考官网):https://pagehelper.github.io/docs/howtouse/
- spring-boot项目在application.yml中进行配置(推荐)
pagehelper:
helper-dialect: mysql //指定数据库语言,不指定会自动检测
- 在mybatis-config-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>
interceptor=”PageHelper类所在包名“
如何获取包名所在的位置?
3. 在Spring的配置文件中进行配置
<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>
参数配置:见官网:https://pagehelper.github.io/docs/howtouse/
3)使用 (分页插件的调用方式)
1)PageHelper.startPage( )方法的使用
(步骤:1、查询几页(设置pageNUm和PageSize) 2、查询数据库(书写查询语句))
好处:不需要在查询语句中设置分页条件
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectIf(1);
list是Page类,并且Page类是继承ArrayList
PageHelper.startPage( ),这个分页方法只对于它的下面的语句进行分页,中间不可以出现其他语句,否则可能报错
其他使用方式遇到更新