开发者

Mybatisplus集成springboot完成分页查询功能(示例代码)

开发者 https://www.devze.com 2023-11-11 10:24 出处:网络 作者: 知识浅谈
目录引入依赖引入myBATisplus依赖分页插件配置类在controller中使用总结今天解决的是:Mybatisplus集成pringboot完成分页功能之前一直用Pagehelper,迫于无奈pagehelper与springboot冲突太多,就改了MP自带的分页
目录
  • 引入依赖
    • 引入myBATisplus依赖
  • 分页插件配置类
    • 在controller中使用
      • 总结

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

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

        引入依赖

        引入mybatisplus依赖

            <dependency>
              <groupId>com.baomidou</groupId>
              <artifactId>mybatis-plus-boot-starter</artifactId>
              <version>3.5.2</version>
            </dependency>

        分页插件配置类

        温馨提醒:这个必不可少

        public class MybatisPlusConfig{
            /**
             * myfHAhXvdbatisplus 分页配置
             */
            @Bean
            public MybatisPlusInterceptor mpInterceptor(){fHAhXvd
                //定义mp拦截器
                MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
                //添加具体的拦截器
                mpInterceptor.ajsddInnerInterceptor(new PaginationInnerInterceptor(DbType.oracle));
                mpInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
                return mpInterceptor;
            }
        }

        在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<javascriptPost> 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;
            }

        总结

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

        到此这篇关于Mybatisplus集成springboot完成分页查询的文章就介绍编程到这了,更多相关Mybatisplus集成springboot内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

        0

        精彩评论

        暂无评论...
        验证码 换一张
        取 消

        关注公众号