首先mybatis plus包含了分页拦截器
我们只需要在springboot 环境中完成就好了
1,首先写mybatis plus的配置类 在类内配置分页拦截器
@Configuration
@ComponentScan("com.example.reggiedemo1.dao")
public class MyBatisPlusConfig {
//这个类是如何识别MybatisPlusInterceptor
/**
* 添加spring-boot与mp结合的依赖后
*这个对象会被springboot识别
* 并自动添加到mp的拦截链上
*/
@Bean
public MybatisPlusInterceptor myBatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
/**
* 添加
* 分页拦截器
*/
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
2,使用分页拦截器 必须使用page对象
定义page对象 将page pageSize相关信息放入
定义查询语句
调用service的page方法将page对象与查询对象放入
Page pageInfo = new Page(page,pageSize);
LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
// 使用降序 按什么降序
lambdaQueryWrapper.orderByDesc(Category::getSort);
iCategoryService.page(pageInfo,lambdaQueryWrapper);