🍁 作者:知识浅谈,CSDN签约讲师,CSDN博客专家,华为云云享专家,阿里云专家博主 📌 擅长领域:全栈工程师、爬虫、ACM算法 💒 公众号:知识浅谈
今天解决的是: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;
}
}
@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;
}
大功告成,撒花致谢🎆🎇🌟,关注我不迷路,带你起飞带你富。