Mybatisplus集成springboot完成分页查询

今天解决的是:Mybatisplus集成pringboot完成分页功能

🛴🛴🛴 之前一直用Pagehelper,迫于无奈pagehelper与springboot冲突太多,就改了MP自带的分页

🎈引入依赖

引入mybatisplus依赖

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

🎈分页插件配置类

温馨提醒:这个必不可少

bash 复制代码
public class MybatisPlusConfig{
    /**
     * mybatisplus 分页配置
     */
    @Bean
    public MybatisPlusInterceptor mpInterceptor(){
        //定义mp拦截器
        MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
        //添加具体的拦截器
        mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
        mpInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return mpInterceptor;
    }

}

🍮在controller中使用

bash 复制代码
    @ApiOperation("分页查询")
    @GetMapping("/pageList")
    public PageResult pageList(@RequestParam(name="postName",required = false) String postName,
                                        @RequestParam(name = "pageNo",required = false) Integer pageNo,
                                        @RequestParam(name = "pageSize",required = false) Integer pageSize){
        PageResult<List<Post>> result = new PageResult<>();
        try {
            if (pageNo == null) pageNo = 1;
            if (pageSize == null) pageSize = 5;
            LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(Post::getPostName,postName);//根据职位名模糊查询
            Page<Post> page = new Page<>(pageNo,pageSize); //定义分页类型
            Page page1 = postService.page(page,queryWrapper); //开始查询
            result.setResult(page1.getRecords());
            result.setTotal(page1.getTotal());
            result.setCurrent(page1.getCurrent());
            result.setPages(page1.getPages());
            result.setSize(page1.getSize());
            result.success("获取职位列表成功!");
        } catch (Exception e) {
            result.error500("获取职位列表失败!");
        }
        return result;
    }

上边看懂就可以过了,看不懂的具体的讲解如下: MyBatis-Plus 是一个在 MyBatis 基础上进行增强的持久层框架,提供了很多便捷的功能,包括分页查询。在 MyBatis-Plus 中,分页查询通常需要使用 Page 对象和 PageHelper 类来实现,以下是使用文字说明的分页使用方法:

  1. 创建一个 Page 对象,指定分页的参数,比如每页显示的记录数和要查询的页码。例如:

    ini 复制代码
    javaCopy Code
    Page<User> page = new Page<>(1, 10); // 查询第1页,每页10条记录
  2. 在进行数据库查询时,将 Page 对象传递给相应的查询方法,并在查询方法中使用 Page 对象进行分页查询。例如:

    ini 复制代码
    javaCopy Code
    IPage<User> userPage = userMapper.selectPage(page, null);
  3. 在查询结果中,可以通过 userPage 对象获取分页查询的结果数据以及分页相关的信息,比如总记录数、总页数等。例如:

    ini 复制代码
    javaCopy Code
    List<User> userList = userPage.getRecords(); // 获取查询结果列表
    long total = userPage.getTotal(); // 获取总记录数
    long current = userPage.getCurrent(); // 获取当前页
    long pages = userPage.getPages(); // 获取总页数

通过以上步骤,就可以在 MyBatis-Plus 中实现分页查询功能。使用 Page 对象可以方便地指定分页参数,而使用 IPage 接口可以获取分页查询的结果数据和分页信息。

希望这些文字说明能够帮助你理解 MyBatis-Plus 中的分页使用方法。如果还有其他问题,欢迎随时提问。

🍚总结

大功告成,撒花致谢🎆🎇🌟,关注我不迷路,带你起飞带你富。

相关推荐
挖土机_0081 分钟前
AI 是否真的能完全替代程序员?从我试用 AI 开发到前后端架构与页面开发的真实分析
后端·ai编程
chenyuhao202423 分钟前
Linux网络编程:HTTP协议
linux·服务器·网络·c++·后端·http·https
Java中文社群1 小时前
避坑指南!别再被N8N循环节点“调戏”了!为什么你的Done分支执行了多次?
人工智能·后端
superman超哥1 小时前
仓颉元编程进阶:编译期计算能力的原理与深度实践
开发语言·后端·仓颉编程语言·仓颉·仓颉语言·仓颉元编程·编译器计算能力
凌览1 小时前
2025年,我和AI合伙开发了四款小工具
前端·javascript·后端
乘风破浪酱524361 小时前
记一次微信小程序登录异常排查:从UnknownHostException到DNS解析失败
后端
先跑起来再说2 小时前
Go 语言的 Mutex 底层实现详解:状态位、CAS、自旋、饥饿模式与信号量
服务器·后端·golang
晴虹2 小时前
lecen:一个更好的开源可视化系统搭建项目--介绍、搭建、访问与基本配置--全低代码|所见即所得|利用可视化设计器构建你的应用系统-做一个懂你的人
前端·后端·低代码
苏叶新城2 小时前
SpringBoot 3.5 JPA投影
java·spring boot·后端