当前位置: 首页 > news >正文

Spring Boot - Spring Boot 集成 MyBatis 分页实现 RowBounds

一、RowBounds 概述

  1. RowBounds 是 MyBatis 提供的纯内存分页实现类,其分页过程完全在内存中完成,不依赖数据库的分页功能

  2. 使用 RowBounds 不需要修改原有 SQL 语句,它会先全量查询,后内存截取

AppMyBatisJDBCDB执行查询(rowBounds)执行原生 SQL查询请求返回完整结果集ResultSet内存分页处理返回分页后的 ListAppMyBatisJDBCDB

二、RowBounds 引入

1、依赖引入
  • pom.xml
<properties>...<postgresql.verison>42.5.6</postgresql.verison><mybatis.version>3.0.1</mybatis.version>
</properties>
<dependencies>...<!-- postgresql 驱动 --><dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><version>${postgresql.verison}</version></dependency><!-- mybatis 和 springboot 整合的起步依赖 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${mybatis.version}</version></dependency>
</dependencies>
2、配置文件
  1. application.yml
mybatis:mapper-locations: classpath:/mapper/*.xml # 映射文件路径config-location: classpath:/mybatis-config.xml # 核心配置文件路径
  1. mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC"-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><!-- 设置驼峰标识 --><setting name="mapUnderscoreToCamelCase" value="true"/><!-- 打印 SQL 语句 --><setting name="logImpl" value="STDOUT_LOGGING"/></settings>
</configuration>
3、准备数据
  1. 创建数据表
CREATE TABLE staff (id SERIAL PRIMARY KEY,name VARCHAR(255) NOT NULL,role VARCHAR(255),salary DECIMAL(10, 2)
);
  1. 插入数据
INSERT INTO staff (name, role, salary) VALUES
('张三', '项目经理', 25000.00),
('李四', '高级开发工程师', 18000.00),
('王五', '开发工程师', 15000.00),
('赵六', '测试工程师', 12000.00),
('钱七', 'UI设计师', 13000.00),
('孙八', '产品经理', 20000.00),
('周九', '运维工程师', 14000.00),
('吴十', '初级开发工程师', 10000.00),
('郑十一', '数据库管理员', 16000.00),
('王十二', '技术总监', 30000.00);

三、RowBounds 分页参数

1、基本介绍
  • 如下是 RowBounds 的源码,其中,它的构造方法接受两个参数
参数说明
offset偏移量
limit查询的条数
public class RowBounds {public static final int NO_ROW_OFFSET = 0;public static final int NO_ROW_LIMIT = Integer.MAX_VALUE;public static final RowBounds DEFAULT = new RowBounds();private final int offset;private final int limit;public RowBounds() {this.offset = NO_ROW_OFFSET;this.limit = NO_ROW_LIMIT;}public RowBounds(int offset, int limit) {this.offset = offset;this.limit = limit;}public int getOffset() {return offset;}public int getLimit() {return limit;}
}
2、演示
// 分页参数计算
int pageNum = 2; // 当前页码
int pageSize = 2; // 每页条数
int offset = (pageNum - 1) * pageSize;// 创建分页参数
RowBounds rowBounds = new RowBounds(offset, pageSize);

四、RowBounds 实例实操

1、SqlSession 使用
(1)Entity
  • Staff.java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Staff {private Integer id;private String name;private String role;private Double salary;
}
(2)Mapper
  1. StaffMapper.java
@Mapper
public interface StaffMapper {List<Staff> queryAll();
}
  1. StaffMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace="com.my.mapper.StaffMapper"><resultMap id="staffResultMap" type="com.my.model.db.Staff"><id column="id" property="id"/><result column="name" property="name"/><result column="role" property="role"/><result column="salary" property="salary"/></resultMap><select id="queryAll" resultMap="staffResultMap">SELECT *FROM staff</select>
</mapper>
(3)Test
// 分页参数计算
int pageNum = 2; // 当前页码
int pageSize = 2; // 每页条数
int offset = (pageNum - 1) * pageSize;// 创建分页参数
RowBounds rowBounds = new RowBounds(offset, pageSize);List<Staff> staffs = sqlSession.selectList("com.my.mapper.StaffMapper.queryAll",null,rowBounds);for (Staff staff : staffs) {System.out.println(staff);
}
# 输出结果Staff(id=5, name=王五, role=开发工程师, salary=15000.0)
Staff(id=6, name=赵六, role=测试工程师, salary=12000.0)
2、Mapper 接口使用
(1)Mapper
  1. StaffMapper.java
List<Staff> queryAllByRowBounds(RowBounds rowBounds);
  1. StaffMapper.xml
<select id="queryAllByRowBounds" resultMap="staffResultMap">SELECT *FROM staff
</select>
(2)Test
// 分页参数计算
int pageNum = 2; // 当前页码
int pageSize = 2; // 每页条数
int offset = (pageNum - 1) * pageSize;// 创建分页参数
RowBounds rowBounds = new RowBounds(offset, pageSize);List<Staff> staffs = staffMapper.queryAllByRowBounds(rowBounds);for (Staff staff : staffs) {System.out.println(staff);
}
# 输出结果Staff(id=5, name=王五, role=开发工程师, salary=15000.0)
Staff(id=6, name=赵六, role=测试工程师, salary=12000.0)
http://www.xdnf.cn/news/1125325.html

相关文章:

  • MySQL高级篇(二):深入理解数据库事务与MySQL锁机制
  • AutoGPT vs BabyAGI:自主任务执行框架对比与选型深度分析
  • 【PTA数据结构 | C语言版】二叉树层序序列化
  • TiD2025 | openKylin基础设施平台创新实践分享,构筑开源质量根基
  • ZYNQ千兆光通信实战:Tri Mode Ethernet MAC深度解析
  • 全面安装指南:在Linux、Windows和macOS上部署Apache Cassandra
  • 基于多智能体强化学习的医疗检索增强生成系统研究—MMOA-RAG架构设计与实现
  • wpf Canvas 动态增加右键菜单
  • Kafka与Flink打造流式数据采集方案:以二手房信息为例
  • 如何设计实现开发自助重启工具-01-设计篇
  • MIPI DSI(四) video 和 command 模式
  • npm install failed如何办?
  • GitHub 上 Star 数量前 8 的开源 Web 应用项目
  • 职业院校网络安全攻防对抗实训室解决方案
  • 微信小程序进度条cavans
  • 2025.7.15总结
  • docker拉取nacos镜像失败
  • GaussDB 数据库架构师修炼(四) 备份容量估算
  • AntV G6 基础元素详解(React版)
  • 邮件伪造漏洞
  • IOS 18下openURL 失效问题
  • 跨平台移动开发技术深度分析:uni-app、React Native与Flutter的迁移成本、性能、场景与前景
  • [Pytest][Part 5]单条测试和用例集测试
  • 【Python3-Django】快速掌握DRF:ModelViewSet实战指南
  • 运维技术教程之Jenkins的秘钥设置
  • Git分支管理与工作流详解
  • ADC采集、缓存
  • HAProxy双机热备,轻松实现负载均衡
  • 聊聊MySQL中的buffer pool
  • 分布式通信框架 - JGroups