Mybatisplus集成springboot完成分页查询功能(示例代码)
作者:知识浅谈
今天小编给大家分享Mybatisplus集成springboot完成分页查询功能,本文通过实例代码给大家介绍的非常详细,需要的朋友参考下吧
今天解决的是: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{
/**
* mybatisplus 分页配置
*/
@Bean
public MybatisPlusInterceptor mpInterceptor(){
//定义mp拦截器
MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
//添加具体的拦截器
mpInterceptor.addInnerInterceptor(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<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;
}🍚总结
大功告成,撒花致谢🎆🎇🌟,关注我不迷路,带你起飞带你富。
到此这篇关于Mybatisplus集成springboot完成分页查询的文章就介绍到这了,更多相关Mybatisplus集成springboot内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
