Mybatis-Plus如何使用分页实例详解
Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生
1.写个Mybatis-plus配置类:
是通过拦截器实现分页
@Configuration public class MybatisConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
官网复制即可,只是你需要把数据库改为你使用的,这里我是使用mysql
2.写接口测试
很简单
@GetMapping("/test") public Response test(){ Page<Produce> producePage = new Page<>(1,1); Page<Produce> page = produceService.page(producePage); System.out.println(producePage == page); List<Produce> records = page.getRecords(); for (Produce record : records) { System.out.println(record); } return new Response<>(records, ResultEnum.SUCCESS); }
默认是会查询总条数,都有get、set方法,可以根据自己的需求设置(点开Page类看看)
3.注意
我们传入的page对象和查询返回的page对象是同一个
4.如果你还有查询条件
比如我们只查询id和price,id小于5的分页查询
1.Lambda表达式
@GetMapping("/test") public Response test(){ Page<Produce> producePage = new Page<>(1,2); Page<Produce> page = new LambdaQueryChainWrapper<>(produceService.getBaseMapper()) .select(Produce::getPid,Produce::getPrice) .lt(Produce::getPid,5) .page(producePage); return new Response<>(page.getRecords(), ResultEnum.SUCCESS); }
2.普通查询
@GetMapping("/test") public Response test(){ Page<Produce> producePage = new Page<>(1,2); QueryWrapper<Produce> queryWrapper = new QueryWrapper<>(); queryWrapper.select("pid","price"); queryWrapper.lt("pid",5); Page<Produce> page = produceService.page(producePage, queryWrapper); return new Response<>(page.getRecords(), ResultEnum.SUCCESS); }
总结
到此这篇关于Mybatis-Plus如何使用分页的文章就介绍到这了,更多相关Mybatis-Plus使用分页内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring boot 自定义 Starter及自动配置的方法
Starter 组件是 Spring boot 的一个核心特性,Starter组件的出现极大的简化了项目开发,这篇文章主要介绍了Spring boot 自定义 Starter 及 自动配置,需要的朋友可以参考下2022-12-12IntelliJ IDEA设置显示内存指示器和设置内存大小的方法
这篇文章主要介绍了IntelliJ IDEA设置显示内存指示器和设置内存大小的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-04-04SpringBoot简单使用SpringData的jdbc和durid
今天给大家带来的是关于Java的相关知识,文章围绕着SpringBoot简单使用SpringData的jdbc和durid,文中有非常详细的介绍及代码示例,需要的朋友可以参考下2021-06-06
最新评论