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); // 这里的方法名应与服务层一致
    }
}
相关推荐
BingoGo3 分钟前
使用 PHP 和 WebSocket 构建实时聊天应用:完整指南
后端·php
JaguarJack18 分钟前
使用 PHP 和 WebSocket 构建实时聊天应用 完整指南
后端·php
苹果醋31 小时前
JAVA设计模式之策略模式
java·运维·spring boot·mysql·nginx
CodeSheep1 小时前
中国四大软件外包公司
前端·后端·程序员
千寻技术帮1 小时前
10370_基于Springboot的校园志愿者管理系统
java·spring boot·后端·毕业设计
风象南1 小时前
Spring Boot 中统一同步与异步执行模型
后端
聆风吟º1 小时前
【Spring Boot 报错已解决】彻底解决 “Main method not found in class com.xxx.Application” 报错
java·spring boot·后端
乐茵lin1 小时前
golang中 Context的四大用法
开发语言·后端·学习·golang·编程·大学生·context
宋情写1 小时前
单元测试、覆盖率测试-Springboot
spring boot·单元测试·测试覆盖率
步步为营DotNet1 小时前
深度探索ASP.NET Core中间件的错误处理机制:保障应用程序稳健运行
后端·中间件·asp.net