这篇文章主要介绍了Mybatis Plus怎么使用分页的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Mybatis Plus怎么使用分页文章都会有所收获,下面我们一起来看看吧。
Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生
是通过拦截器实现分页
@Configuration
public class MybatisConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
官网复制即可,只是你需要把数据库改为你使用的,这里我是使用mysql
很简单
@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类看看)
我们传入的page对象和查询返回的page对象是同一个
比如我们只查询id和price,id小于5的分页查询
@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);
}
@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怎么使用分页”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。