• Mybatisplus集成springboot完成分页查询


    🍁 作者:知识浅谈,CSDN签约讲师,CSDN博客专家,华为云云享专家,阿里云专家博主
    📌 擅长领域:全栈工程师、爬虫、ACM算法
    💒 公众号:知识浅谈
    🔥网站:vip.zsqt.cc

    今天解决的是: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

    🍚总结

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

  • 相关阅读:
    数字后端——物理单元介绍(一)
    十大排序算法之——冒泡排序算法(Java实现)及思路讲解
    【图像分割】DeepLabV3+
    【机器学习10】循环神经网络
    高通Android 12默认授权 不弹出投屏弹窗
    豆瓣影评信息爬取 (爬虫)
    OpenGL - Parallax Mapping
    代码随想录算法训练营第十三天|n皇后&数独老经典了
    基于Java的4S店汽车商城系统设计与实现(源码+lw+部署文档+讲解等)
    趣测系统搭建APP源码开发,娱乐丰富生活的选择!
  • 原文地址:https://blog.csdn.net/qq_37699336/article/details/134325701