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 // 数据总量
}
相关推荐
北京_宏哥几秒前
《刚刚问世》系列初窥篇-Java+Playwright自动化测试-27- 操作单选和多选按钮 - 上篇(详细教程)
java·前端·面试
回家路上绕了弯20 分钟前
Java双亲委派机制:从原理到实践的全面解析
java·后端
努力的小郑22 分钟前
亿级流量下的生死抉择:Apache BeanUtils vs MapStruct性能差距32倍!架构师选型指南
java·spring·apache
努力的小郑22 分钟前
BeanUtils拷贝大对决:Spring与Apache Commons的差异与妙用
java·spring·apache
guojl23 分钟前
MyBatis应用案例
后端·mybatis
求知摆渡28 分钟前
Spring Boot 3.5 + Spring Cloud Stream:邮件发送与幂等实战
java·spring boot·spring cloud
用户400784221126030 分钟前
苍穹外卖实现员工账号启用禁用
java
中东大鹅34 分钟前
Mybatis Plus 多数据源
java·数据库·spring boot·后端·mybatis
用户40078422112601 小时前
苍穹外卖实现员工分页查询
java
Code季风1 小时前
深入理解令牌桶算法:实现分布式系统高效限流的秘籍
java·算法·微服务