SpringBoot + MyBatis-Plus 实现分页操作详解

在SpringBoot项目中,结合MyBatis-Plus(简称MP)可以非常方便地实现分页功能。MP为开发者提供了分页插件PaginationInterceptor,只需简单配置即可使用。

一、配置分页插件

首先,在SpringBoot的配置类中(通常是带有@Configuration注解的类)配置PaginationInterceptor

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(DbType.H2)); // 根据你的数据库类型选择相应的DbType
        return interceptor;
    }
}

注意:在新版本的MyBatis-Plus中,分页插件的配置方式有所变化,不再使用PaginationInterceptor,而是使用MybatisPlusInterceptor结合PaginationInnerInterceptor。请根据你的MP版本选择合适的配置方式。

二、编写Mapper接口

接下来,在Mapper接口中定义分页查询的方法。不需要在Mapper的XML文件中编写SQL语句来实现分页,因为分页插件会自动对SQL进行分页处理。

java 复制代码
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Select;
import your.package.name.EntityClass; 

public interface YourMapper extends BaseMapper<EntityClass> {
    // 使用MP提供的分页功能,不需要编写具体的分页SQL
    IPage<EntityClass> selectPageList(Page<EntityClass> page, QueryWrapper<EntityClass> queryWrapper);
}

实际上,如果你的EntityClass正确继承了MP的BaseModel(在新版本中通常不需要继承),并且你的Mapper继承了BaseMapper,你甚至不需要在Mapper接口中定义上述的selectPageList方法,因为BaseMapper已经提供了分页查询的方法。

三、在服务层调用分页查询

在服务层,你可以调用Mapper接口中的分页查询方法。首先,你需要创建一个Page对象,并设置当前页码和每页显示的记录数。然后,调用Mapper的方法执行分页查询。

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

@Service
public class YourService {
    @Autowired
    private YourMapper yourMapper;
    
    public Page<EntityClass> getPageList(int current, int size) {
        // 创建分页对象,current为当前页码,size为每页显示的数量
        Page<EntityClass> page = new Page<>(current, size);
        // 调用Mapper方法进行分页查询,这里假设没有额外的查询条件,如果有可以使用QueryWrapper添加条件
        return yourMapper.selectPage(page, null); // 如果你的Mapper没有定义selectPageList方法,请使用BaseMapper中的selectPage方法代替
    }
}

请注意:如果你的MP版本较新,且你的实体没有继承MP的任何类(这是推荐的做法),你应该直接使用BaseMapper中的selectPage方法。此外,如果你的查询需要额外的条件,可以创建一个QueryWrapper对象来指定这些条件。在上面的示例中,我没有添加任何条件,所以传递了null作为第二个参数。

四、在控制器中调用服务层的分页方法

最后,在控制器中调用服务层的分页方法,并将结果返回给前端。通常,前端会期望得到一个包含当前页码、总页数、每页记录数和数据列表的响应对象。你可以自定义一个响应类来封装这些信息,或者直接使用MP提供的Page对象。这里演示后者。

java 复制代码
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import your.package.name.EntityClass; 
import your.package.name.YourService; 

@RestController
@RequestMapping("/your-endpoint")
public class YourController {
    @Autowired
    private YourService yourService;
    
    @GetMapping("/page") // 分页查询的API路径,例如/your-endpoint/page?current=1&size=10
    public Page<EntityClass> getPageList(@RequestParam int current, @RequestParam int size) {
        // 调用服务层方法进行分页查询,并返回结果给前端
        return yourService.getPageList(current, size); // 这里的方法名应与服务层一致
    }
}
相关推荐
小马爱打代码21 分钟前
Spring源码 第四篇:Spring 5 源码深度拆解:AOP 全流程核心原理
java·后端·spring
ServBay1 小时前
2026 Mac 本地大模型部署深度解析与混合架构指南
后端·macos·aigc
一拳一个娘娘腔1 小时前
【SRC漏洞挖掘系列】第10期:GraphQL & API 安全 —— 现代 API 的“裸奔”时代
后端·安全·graphql
ZhengEnCi2 小时前
01-如何监听接口调用情况?
java·spring boot·后端
JAVA面经实录9173 小时前
MyBatis学习体系
java·mybatis
苏渡苇3 小时前
Spring Cloud Alibaba:将 Sentinel 熔断限流规则持久化到 Nacos 配置中心
数据库·spring boot·mysql·spring cloud·nacos·sentinel·持久化
小马爱打代码3 小时前
Spring源码 第九篇:Spring 5 源码深度拆解 - Spring 事件驱动模型
java·后端·spring
ForgeAI码匠4 小时前
ForgeAdmin|Spring Boot 3 后台框架的自动配置设计:少写配置,多做组合
java·spring boot·后端
墨_风4 小时前
MyBatis时间区间查询异常排查(达梦数据库)
数据库·mybatis·达梦
IT_陈寒4 小时前
为什么 Java 的 Optional 让我调试到深夜?
前端·人工智能·后端