前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatisplus集成springboot完成分页查询

Mybatisplus集成springboot完成分页查询

作者头像
知识浅谈
发布2023-11-11 09:42:02
2040
发布2023-11-11 09:42:02
举报
文章被收录于专栏:分享学习分享学习

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

今天解决的是:Mybatisplus集成pringboot完成分页功能 🛴🛴🛴 之前一直用Pagehelper,迫于无奈pagehelper与springboot冲突太多,就改了MP自带的分页

🎈引入依赖

引入mybatisplus依赖

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

🎈分页插件配置类

温馨提醒:这个必不可少

代码语言:javascript
复制
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中使用
代码语言:javascript
复制
    @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;
    }

🍚总结

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-11-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 🎈引入依赖
  • 🎈分页插件配置类
    • 🍮在controller中使用
    • 🍚总结
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档