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

相关推荐
双力臂4042 小时前
MyBatis动态SQL进阶:复杂查询与性能优化实战
java·sql·性能优化·mybatis
慕y2745 小时前
Java学习第十五部分——MyBatis
java·学习·mybatis
一勺菠萝丶12 小时前
Spring Boot + MyBatis/MyBatis Plus:XML中循环处理List参数的终极指南
xml·spring boot·mybatis
coding and coffee15 小时前
狂神说 - Mybatis 学习笔记 --下
java·后端·mybatis
耀耀_很无聊1 天前
07_通过 Mybatis 自动填充记录的创建时间和更新时间
mybatis
程序员张31 天前
SQL分析与打印-p6spy组件
spring boot·sql·mybatis·mybatisplus·p6spy
喜欢敲代码的程序员2 天前
SpringBoot+Mybatis+MySQL+Vue+ElementUI前后端分离版:项目搭建(一)
spring boot·mysql·elementui·vue·mybatis
一只猿Hou2 天前
java分页插件| MyBatis-Plus分页 vs PageHelper分页:全面对比与最佳实践
java·mybatis
java—大象12 天前
基于java SSM的房屋租赁系统设计和实现
java·开发语言·数据库·spring boot·layui·mybatis