PageHelper分页插件

时间:2022-07-28
本文章向大家介绍PageHelper分页插件,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1 概念

PageHelper是适用于MyBatis框架的一个分页插件,使用方式极为便捷,支持任何复杂的单表、多表分页查询操作。

2 访问与下载

官方网站:https://pagehelper.github.io/

下载地址:https://github.com/pagehelper/Mybatis-PageHelper

3. 开发步骤

3.1 pom.xml中引入PageHelper依赖。

<dependency>
		<groupId>com.github.pagehelper</groupId>
		<artifactId>pagehelper</artifactId>
		<version>5.1.10</version>
</dependency>

3.2 配置MyBatis-config.xml,在MyBatis-config.xml中添加

<configuration>
  	<typeAliases></typeAliases>
  
    <plugins>
        <!-- com.github.pagehelper为PageHelper类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>
  
  	<environments>...</environments>
</configuration>

3.3 使用PageHelper提供的静态方法设置分页查询条件。

@Test
public void testPagehelper(){
		UserDao userDao = MyBatisUtils.getMapper(UserDao.class);
		PageHelper.startPage(1,2);//使用PageHelper设置分页条件
		List<User> users = userDao.selectAllUsers();
		for(User user : users){
				System.out.println(user);
		}
}

3.4PageInfo对象

3.5注意事项

- 只有在PageHelper.startPage()方法之后的第一个查询会有执行分页。
- 分页插件不支持带有“for update”的查询语句。
- 分页插件不支持“嵌套查询”,由于嵌套结果方式会导致结果集被折叠,所以无法保证分页结果数量正确。