提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
Mybatis-Plus分页插件的使用
首先创建一个Mybatis-plus配置类,如下图所示:
在配置类中添加分页插件组件
@Configuration
@MapperScan("com.xxx.xxx.mapper") //你的mapper文件
public class MybatisPlusConfig {
//分页插件
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
在Impl实现类中添加,主要有两个参数,第一个pageParams.getPage()用来指明第几页,pageParams.getPageSize()用来指定每页的记录数
//创建Page对象
Page<Article> page = new Page<>(pageParams.getPage(), pageParams.getPageSize());
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
//添加条件
queryWrapper.eq(xx)
queryWrapper.orderByDesc(xx)
//传入page对象和条件
Page<Article> articlePage = articleMapper.selectPage(page, queryWrapper);
List<Article> records = articlePage.getRecords();