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 接口。

相关推荐
菲力蒲LY9 小时前
输入搜索、分组展示选项、下拉选取,全局跳转页,el-select 实现 —— 后端数据处理代码,抛砖引玉展思路
java·前端·mybatis
五月茶11 小时前
Maven+SSM+SpringBoot+Mybatis-Plus
spring boot·maven·mybatis
B站计算机毕业设计超人12 小时前
计算机毕业设计SpringBoot+Vue.jst房屋租赁系统(源码+LW文档+PPT+讲解)
vue.js·spring boot·后端·eclipse·intellij-idea·mybatis·课程设计
天上掉下来个程小白1 天前
案例-14.文件上传-简介
数据库·spring boot·后端·mybatis·状态模式
sunnyday04261 天前
MyBatis XML映射文件中的批量插入和更新
xml·java·mysql·mybatis
剑走偏锋o.O3 天前
Java四大框架深度剖析:MyBatis、Spring、SpringMVC与SpringBoot
java·spring boot·spring·mybatis
风月歌3 天前
基于springboot校园健康系统的设计与实现(源码+文档)
java·spring boot·后端·mysql·毕业设计·mybatis·源码
剑走偏锋o.O3 天前
MyBatis框架详解与核心配置解读
java·学习·mybatis
ONEPEICE-ing3 天前
快速入门Springboot+vue——MybatisPlus多表查询及分页查询
前端·vue.js·spring boot·mybatis