使用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中测试

相关推荐
神奇的程序员1 小时前
开发了一个管理本地开发环境的软件
前端·flutter
qq_589568102 小时前
springbootweb案例,出现访问 http://localhost:8080/list 一直处于浏览器运转阶段
java·网络协议·http·list·springboot
JAVA面经实录9172 小时前
计算机基础(完整版·超详细可背诵)
java·linux·数据结构·算法
XiYang-DING2 小时前
HTML 核心标签
前端·html
AC赳赳老秦2 小时前
知识产权辅助:用 OpenClaw 批量生成专利交底书 / 软著申请材料,自动校验格式与内容合规性
java·人工智能·python·算法·elasticsearch·deepseek·openclaw
Csvn2 小时前
技术选型方法论
前端
Csvn2 小时前
前端架构演进:从页面到平台的十年变革
前端
李伟_Li慢慢3 小时前
ShaderToy-山峦+蓝天+白云
前端·webgl
小码哥_常3 小时前
Android字体字重设置全攻略:XML黑科技+Kotlin动态实现,告别.ttf臃肿
前端