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 需要替换为你的实际包名,YourEntity 和 YourEntityMapper 需要替换为你的实际实体类和 Mapper 接口。

相关推荐
乌暮2 天前
少写重复 CRUD:用大白话搞懂 MyBatis-Plus
java·开发语言·spring boot·面试·mybatis
CoderYanger2 天前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第四弹):图书管理系统(用户登录+添加图书)
java·数据库·spring boot·mysql·面试·java-ee·mybatis
牧瀬クリスだ2 天前
从零手写博客系统:分层架构与统一响应(一)
java·数据库·架构·mybatis·springboot·安全架构
CoderYanger3 天前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第二弹)
java·数据库·面试·职场和发展·java-ee·tomcat·mybatis
CoderYanger3 天前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第一弹)
java·数据库·spring·面试·java-ee·tomcat·mybatis
未秃头的程序猿3 天前
给新模块加了个 @MapperScan,启动崩了,最后发现跟 MapperScan 没关系
java·后端·mybatis
阿达和自己7 天前
springboot学习-springboot引入MyBatis-Plus
spring boot·学习·mybatis
2603_969694858 天前
【JavaWeb学习记录】MyBatis核心知识点与实战笔记:从入门到动态SQL
笔记·sql·学习·mysql·mybatis·javaweb·学习方法
wang_shu_mo_ran8 天前
MyBatis 从入门到进阶(一)
java·spring·mybatis
瑞雪兆丰年兮8 天前
JavaWeb-day14-SpringBootWeb原理篇
java·开发语言·spring boot·spring·bean·mybatis·javaweb