MyBatis-Plus分页插件的使用

从MyBatis-Plus 3.4.0开始,不再使用旧版本的PaginationInterceptor ,而是使用MybatisPlusInterceptor。

下面是MyBatis-Plus 3.4.3.3新版分页的使用方法。

配置

使用分页插件需要配置MybatisPlusInterceptor,将分页拦截器添加进来:

javascript 复制代码
@Configuration
public class MyBatisPlusConfig {

    /**
     * 分页插件配置
     *
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 向MyBatis-Plus的过滤器链中添加分页拦截器,需要设置数据库类型(主要用于分页方言)
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

使用分页功能

和分页功能有关的类是Page类,构造分页参数的方法:

javascript 复制代码
Page<UserEntity> page = new Page<>(1, 1);

第一个参数是页码(从1开始),第二个参数是分页大小。

javascript 复制代码
使用的时候只需要将创建的Page对象作为第一个参数传入即可。

首先,定义接口:

javascript 复制代码
@Mapper
public interface UserMapper extends BaseMapper<UserEntity> {

    /**
     * 测试分页插件
     *
     * @param page
     * @return
     */
    Page<UserEntity> testPage(Page<UserEntity> page);
}

然后,编写SQL:

javascript 复制代码
<?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.tao.adminserver.mapper.UserMapper">

    <!-- 配置数据库字段和Java类中字段的映射关系 -->
    <resultMap id="baseResultMap" type="com.tao.adminserver.entity.UserEntity">
        <id column="id" property="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result column="name" property="name" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result column="age" property="age" jdbcType="INTEGER" javaType="java.lang.Integer"/>
        <result column="email" property="email" jdbcType="VARCHAR" javaType="java.lang.String"/>
    </resultMap>


    <!-- 测试分页插件 -->
    <select id="testPage" resultMap="baseResultMap">
        select * from tb_user
    </select>

</mapper>

SQL中不需要用limit来手动分页,分页插件会自己加上的。

最后,来测试一下:

控制台打印的日志:

javascript 复制代码
==>  Preparing: SELECT COUNT(*) FROM tb_user
==> Parameters: 
<==    Columns: COUNT(*)
<==        Row: 3
<==      Total: 1
==>  Preparing: select * from tb_user LIMIT ?
==> Parameters: 2(Long)
<==    Columns: id, name, age, email
<==        Row: 7, Mike, 40, test7@baomidou.com
<==        Row: 8, Tank, 40, test8@baomidou.com
<==      Total: 2

......

==>  Preparing: SELECT COUNT(*) FROM tb_user
==> Parameters: 
<==    Columns: COUNT(*)
<==        Row: 3
<==      Total: 1
==>  Preparing: select * from tb_user LIMIT ?,?
==> Parameters: 2(Long), 2(Long)
<==    Columns: id, name, age, email
<==        Row: 9, Adele, 22, test9@baomidou.com
<==      Total: 1

可以看出,分页之后,首先count计算了总数,然后在查询的时候自动加上了limit语句。

分页结果对象如下:

javascript 复制代码
{
	"current": 1, // 当前是第几页
	"optimizeCountSql": true,
	"orders": [],
	"pages": 2,  // 总共有几页
	"records": [ // 当前页的记录详细信息
		{
			"age": 40,
			"email": "test7@baomidou.com",
			"id": 7,
			"name": "Mike"
		},
		{
			"age": 40,
			"email": "test8@baomidou.com",
			"id": 8,
			"name": "Tank"
		}
	],
	"searchCount": true,
	"size": 2, // 当前页的数据量(分页大小)
	"total": 3 // 数据总量
}
相关推荐
SamDeepThinking7 分钟前
第1篇-开篇词:几亿用户规模下,我们是怎么做C端高并发商品系统的
java·后端·架构
weisian1518 分钟前
Java并发编程--47-分布式ID生成器:雪花算法(Snowflake)与时钟回拨问题
java·算法·时钟回拨·雪花算法id
itzixiao9 分钟前
L1-066 猫是液体(5分)[java][python]
java·开发语言·python·算法
冷小鱼16 分钟前
MyBatis 与 MyBatis-Plus:从入门到精通的完整指南
java·tomcat·mybatis
DolphinScheduler社区26 分钟前
DolphinScheduler 3.3.2 如何调用 DataX 3.0 + SeaTunnel 2.3.12?附 Demo演示!
java·spark·apache·海豚调度·大数据工作流调度
亦暖筑序1 小时前
AI 客服系统安全加固:JWT 鉴权 + Bucket4j 三层限流
java·架构
xhuiting1 小时前
项目技术总结
java
某人辛木1 小时前
JDK安装配置
java·开发语言
counting money1 小时前
Spring框架基础(依赖注入-全注解形式)
java·数据库·spring
小王师傅661 小时前
【Java结构化梳理】泛型-初步了解-下
java·开发语言