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); // 这里的方法名应与服务层一致
    }
}
相关推荐
子兮曰2 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰2 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
爱勇宝2 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
胡写代码2 天前
别再前后端各写一套表单校验了
java·后端
大勇前进2 天前
原生 PHP 还是 Laravel?小项目到底要不要上框架
后端
vipxieliang2 天前
ValidX 在 DDD 领域驱动设计中的实践
java·spring boot
yuzhi_liu2 天前
我用 LangGraph4j 实现 Multi-Agent Supervisor
后端
alsmile2 天前
Node-RED 之外,国产规则引擎的新方案:基于标准语法,Go 先行实现
后端·开源·go
大白802 天前
PHP 内存溢出排查思路:看懂报错日志,精准定位问题
后端
二月龙2 天前
PHP 接口返回统一响应封装,让前后端对接更省心
后端