mybatis-plus 实现分页查询步骤

MyBatis-Plus 是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。它提供了代码生成器、条件构造器、分页插件等多种功能,其中分页查询是一个常用的功能。

以下是如何在 MyBatis-Plus 中实现分页查询的基本步骤:

1. 引入 MyBatis-Plus 分页插件依赖

首先,确保你的项目中已经添加了 MyBatis-Plus 的依赖,并且包含了分页插件。如果没有,可以在 pom.xml 中添加如下依赖:

xml

java 复制代码
<!-- MyBatis-Plus 分页插件 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>你的MyBatis-Plus版本</version>
</dependency>

2. 配置分页插件

在你的 Spring Boot 配置类中,添加分页插件的配置:

java

java 复制代码
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}

3. 使用分页查询

在你的 Mapper 接口中,你可以使用 MyBatis-Plus 提供的 IPage<T> 类型来接收分页参数,并返回分页结果。

java

java 复制代码
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import your.package.name.entity.YourEntity;

@Mapper
public interface YourEntityMapper extends BaseMapper<YourEntity> {
    // 这里定义你的 CRUD 操作
}

在你的服务层或控制器层,你可以这样使用分页查询:

java

java 复制代码
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import your.package.name.mapper.YourEntityMapper;
import your.package.name.entity.YourEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class YourEntityService {

    @Autowired
    private YourEntityMapper yourEntityMapper;

    public Page<YourEntity> selectPage(int current, int size) {
        Page<YourEntity> page = new Page<>(current, size);
        return yourEntityMapper.selectPage(page, null); // 第二个参数可以是查询条件,这里为 null 表示查询所有
    }
}

4. 控制器层调用

在你的控制器中,你可以接收前端传递的分页参数,并调用服务层的方法:

java

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import your.package.name.entity.YourEntity;
import your.package.name.service.YourEntityService;

import java.util.List;

@RestController
public class YourEntityController {

    @Autowired
    private YourEntityService yourEntityService;

    @GetMapping("your-entity/list")
    public Page<YourEntity> list(@RequestParam(defaultValue = "1") int current,
                                 @RequestParam(defaultValue = "10") int size) {
        return yourEntityService.selectPage(current, size);
    }
}

这样,当请求到达控制器的 list 方法时,就会执行分页查询,并返回分页结果。

请注意,这里的 your.package.name 需要替换为你的实际包名,YourEntityYourEntityMapper 需要替换为你的实际实体类和 Mapper 接口。

相关推荐
没有bug.的程序员8 小时前
Spring Boot 整合第三方组件:Redis、MyBatis、Kafka 实战
java·spring boot·redis·后端·spring·bean·mybatis
迷了璐的航9 小时前
mybatis解决查询中使用group by时出现sql_mode=only_full_group_by
数据库·sql·mybatis
悟能不能悟10 小时前
mybatis的sql中,如果一个条件column=#{column},column的值为null时,会不会匹配出column is null 的记录
sql·tomcat·mybatis
技术砖家--Felix11 小时前
Spring Boot数据访问篇:整合MyBatis操作数据库
数据库·spring boot·mybatis
寒月霜华13 小时前
JavaWeb后端-JDBC、MyBatis
spring boot·junit·mybatis
艾菜籽1 天前
MyBatis动态sql与留言墙联系
java·数据库·sql·spring·mybatis
l0sgAi2 天前
PageHelper-Cursor:支持游标分页的 PageHelper 修改版,解决 MyBatis 深分页性能问题
spring boot·mybatis
计算机学姐2 天前
基于SpringBoo+Vue的医院预约挂号管理系统【个性化推荐算法+可视化统计】
java·vue.js·spring boot·mysql·intellij-idea·mybatis·推荐算法
计算机学姐2 天前
基于微信小程序的奶茶店点餐平台【2026最新】
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
后端小张3 天前
【JAVA 进阶】Mybatis-Plus 实战使用与最佳实践
java·spring boot·spring·spring cloud·tomcat·mybatis·mybatis plus