• Mybatisplus集成springboot完成分页查询


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

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

    🎈引入依赖

    引入mybatisplus依赖

        <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus-boot-starter</artifactId>
          <version>3.5.2</version>
        </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    🎈分页插件配置类

    温馨提醒:这个必不可少

    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;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    🍮在controller中使用

        @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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

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

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

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

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

      javaCopy Code
      List userList = userPage.getRecords(); // 获取查询结果列表
      long total = userPage.getTotal(); // 获取总记录数
      long current = userPage.getCurrent(); // 获取当前页
      long pages = userPage.getPages(); // 获取总页数
      
      • 1
      • 2
      • 3
      • 4
      • 5

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

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

    🍚总结

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

  • 相关阅读:
    Java之Spring面试总结
    计算机网络 交换机的VLAN配置
    git基础操作
    多线程基础
    vue3 mouse.js 鼠标的位置
    Bearpi开发板HarmonyOS之GPIO中断
    基于JAVA旅游景点推荐系统计算机毕业设计源码+数据库+lw文档+系统+部署
    使用 .NET 8.0 和 OpenGL 创建一个简易的渲染器
    【iOS逆向与安全】插件开发之某音App直播间自动发666
    Android简易音乐重构MVVM Java版 -搭建项目(八)
  • 原文地址:https://blog.csdn.net/qq_37699336/article/details/134425722