使用xml编写查询前提下MyBatisPlus分页IPage用法

在pom文件里增加MyBatisPlus依赖

复制代码
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3.1</version>
</dependency>

在config层定义拦截器

复制代码
@Configuration
public class MybatisPlusConfig {
    //定义一个mybatisPlus的拦截器 再 add一个分页拦截器
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        //1.初始化核心插件
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //2.添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }

}

在mapper层调用xml中的查询语句,并加上参数

复制代码
IPage<User> getOrdersByUser(Page<User> page);

在service定义一个方法实现分页,这里传入两个参数,current和size,current表示当前页码,size表示每页几行

复制代码
IPage<User> getUserByPage(int current, int size);

实现接口

复制代码
    @Override
    public IPage<User> getUserByPage(int current, int size) {
        Page<User> page = new Page<>(current,size);
        return userMapper.getOrdersByUser(page);
    }

在control层调用接口方法

复制代码
@GetMapping("/users")
public IPage<User> getPage( @RequestParam(value = "current", defaultValue = "1") int current,
                            @RequestParam(value = "size", defaultValue = "2") int size){
    return userServiceImpl.getUserByPage(current,size);
}

在postman中测试

相关推荐
ss27312 分钟前
食谱推荐系统功能测试如何写?
java·数据库·spring boot·功能测试
2301_8112743125 分钟前
基于SpringBoot的智能家居管理系统
spring boot·后端·智能家居
毕设源码_古学姐29 分钟前
计算机毕业设计springboot智能家居项目管理系统 基于SpringBoot的智能家居项目管理平台设计与实现 SpringBoot技术驱动的智能家居项目管理系统开发
spring boot·智能家居·课程设计
毕设源码-张学姐32 分钟前
计算机毕业设计springboot智能家居设备信息管理系统 基于SpringBoot的智能家居设备全生命周期管理平台 面向智慧家庭的SpringBoot设备资产与场景运营系统
spring boot·智能家居·课程设计
AI人工智能+电脑小能手32 分钟前
【大白话说Java面试题】【Java基础篇】第15题:JDK1.7中HashMap扩容为什么会发生死循环?如何解决
java·开发语言·数据结构·后端·面试·哈希算法
try2find1 小时前
打印ascii码报错问题
java·linux·前端
014-code1 小时前
CompletableFuture 实战模板(超时、组合、异常链处理)
java·数据库
Nicander1 小时前
多数据源下@transcation事务踩坑
java·后端
郑州光合科技余经理1 小时前
同城O2O海外版二次开发实战:从支付网关到配送算法
开发语言·前端·后端·算法·架构·uni-app·php
それども2 小时前
DELETE 和 TRUNCATE TABLE区别
java·数据库·mysql