周振林 周振林
首页
  • 前端文章

    • HTML
    • CSS
    • Tailwind CSS (opens new window)
    • JavaScript
    • Vue3
    • 其他
  • Spring
  • SpringMVC
  • Mybatis
  • 安装教程
  • 其他教程
  • 基础
  • 虚拟化
  • Docker
  • OpenStack
  • 心情杂货
关于
收藏

周振林

IT界的小学生
首页
  • 前端文章

    • HTML
    • CSS
    • Tailwind CSS (opens new window)
    • JavaScript
    • Vue3
    • 其他
  • Spring
  • SpringMVC
  • Mybatis
  • 安装教程
  • 其他教程
  • 基础
  • 虚拟化
  • Docker
  • OpenStack
  • 心情杂货
关于
收藏
  • Spring

  • SpringMVC

  • Mybatis

    • Mybatis CRUD
    • Mybatis参数
    • Mybatis返回类型
    • Mybatis动态SQL
    • Mybatis分页插件
      • Mybatis分页插件
        • 插件机制
        • PageHelper分页插件
        • 案例
    • Mybatis 其他
  • 安装教程

  • 其他教程

  • 后端
  • Mybatis
周振林
2025-12-02
目录

Mybatis分页插件

# Mybatis分页插件

# 插件机制

MyBatis 底层使用 拦截器机制提供插件功能,方便用户在SQL执行前后进行拦截增强。
拦截器:Interceptor
拦截器可以拦截 四大对象 的执行
ParameterHandler:处理SQL的参数对象
ResultSetHandler:处理SQL的返回结果集
StatementHandler:数据库的处理对象,用于执行SQL语句
Executor:MyBatis的执行器,用于执行增删改查操作

# MyBatis 插件机制:基于拦截器的增强能力

# PageHelper分页插件

PageHelper 是可以用在 MyBatis 中的一个强大的分页插件
分页插件就是利用MyBatis 插件机制,在底层编写了 分页Interceptor,每次SQL查询之前会自动拼装分页数据

select * from emp limit 0,10
分页重点:
前端 第1页: limit 0,10
前端 第2页: limit 10,10
前端 第3页: limit 20,10
前端 第N页:limit startIndex,pageSize
计算规则: pageNum = 1, pageSize = 10
startIndex = (pageNum - 1)*pageSize

# 案例

pom.xml

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

MyBatisConfig

//@MapperScan("com.atguigu.mybatis.mapper") //批量只扫描mapper
@Configuration
public class MyBatisConfig {

    @Bean
    PageInterceptor pageInterceptor(){
        //1、创建 分页插件 对象
        PageInterceptor interceptor = new PageInterceptor();
        //2、设置 参数
        Properties properties = new Properties();
        properties.setProperty("reasonable", "true");

        interceptor.setProperties(properties);
        return interceptor;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

或yml配置文件

pagehelper:
  reasonable: true       # 分页合理化(页码 <=0 查第一页,页码 > 总页数查最后一页)
  support-methods-arguments: true  # 支持通过 Mapper接口参数传递分页参数
1
2
3

PageInfo 封装分页信息
controller

@GetMapping("/emp/page")
public PageInfo getPage(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
    PageHelper.startPage(pageNum, 5);
    List<Emp> all = empService.getAll();
    return new PageInfo<>(all);
}
1
2
3
4
5
6
Last Updated: 2025/12/02, 11:22:00
Mybatis动态SQL
Mybatis 其他

← Mybatis动态SQL Mybatis 其他→

最近更新
01
查询优化N+1
12-02
02
项目代码组织方式
12-02
03
Mybatis动态SQL
12-02
更多文章>
Copyright © 2019-2025 鲁ICP备19032096号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×