Spring Boot - Spring Boot 集成 MyBatis 分页实现 RowBounds
一、RowBounds 概述
-
RowBounds 是 MyBatis 提供的纯内存分页实现类,其分页过程完全在内存中完成,不依赖数据库的分页功能
-
使用 RowBounds 不需要修改原有 SQL 语句,它会先全量查询,后内存截取
二、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、配置文件
- application.yml
mybatis:mapper-locations: classpath:/mapper/*.xml # 映射文件路径config-location: classpath:/mybatis-config.xml # 核心配置文件路径
- 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、准备数据
- 创建数据表
CREATE TABLE staff (id SERIAL PRIMARY KEY,name VARCHAR(255) NOT NULL,role VARCHAR(255),salary DECIMAL(10, 2)
);
- 插入数据
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
- StaffMapper.java
@Mapper
public interface StaffMapper {List<Staff> queryAll();
}
- 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
- StaffMapper.java
List<Staff> queryAllByRowBounds(RowBounds rowBounds);
- 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)