Mybatis Plus怎么使用分页
导读:本文共1850.5字符,通常情况下阅读需要6分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: Mybatis-Plus(简称MP)是一个Mybatis的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生1.写个Mybatis-plus配置类:是通过拦截器实现分页@ConfigurationpublicclassMybatisConfig{@BeanpublicMybatisPlusInterceptormybatisP... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。Mybatis-Plus(简称MP)是一个Mybatis的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生
1.写个Mybatis-plus配置类:
是通过拦截器实现分页
@ConfigurationpublicclassMybatisConfig{@BeanpublicMybatisPlusInterceptormybatisPlusInterceptor(){MybatisPlusInterceptorinterceptor=newMybatisPlusInterceptor();interceptor.addInnerInterceptor(newPaginationInnerInterceptor(DbType.MYSQL));returninterceptor;}}
官网复制即可,只是你需要把数据库改为你使用的,这里我是使用mysql
2.写接口测试
很简单
@GetMapping("/test")publicResponsetest(){Page<Produce>producePage=newPage<>(1,1);Page<Produce>page=produceService.page(producePage);System.out.println(producePage==page);List<Produce>records=page.getRecords();for(Producerecord:records){System.out.println(record);}returnnewResponse<>(records,ResultEnum.SUCCESS);}
默认是会查询总条数,都有get、set方法,可以根据自己的需求设置(点开Page类看看)
3.注意
我们传入的page对象和查询返回的page对象是同一个
4.如果你还有查询条件
比如我们只查询id和price,id小于5的分页查询
1.Lambda表达式
@GetMapping("/test")publicResponsetest(){Page<Produce>producePage=newPage<>(1,2);Page<Produce>page=newLambdaQueryChainWrapper<>(produceService.getBaseMapper()).select(Produce::getPid,Produce::getPrice).lt(Produce::getPid,5).page(producePage);returnnewResponse<>(page.getRecords(),ResultEnum.SUCCESS);}
2.普通查询
@GetMapping("/test")publicResponsetest(){Page<Produce>producePage=newPage<>(1,2);QueryWrapper<Produce>queryWrapper=newQueryWrapper<>();queryWrapper.select("pid","price");queryWrapper.lt("pid",5);Page<Produce>page=produceService.page(producePage,queryWrapper);returnnewResponse<>(page.getRecords(),ResultEnum.SUCCESS);}
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
Mybatis Plus怎么使用分页的详细内容,希望对您有所帮助,信息来源于网络。