1、引入jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.3</version>
</dependency>
2、配置pagehelper
找到项目中数据源配置信息
加入如下配置
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
其他配置信息........
// 设置合理分页参数等配置
PageInterceptor pageInterceptor = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("reasonable", "true");
properties.setProperty("helperDialect", "mysql");
properties.setProperty("supportMethodsArguments", "true");
properties.setProperty("params", "count=countSql");
pageInterceptor.setProperties(properties);
sessionFactory.setPlugins(new Interceptor[]{pageInterceptor});
// mybatis 拦截插件
// sessionFactory.setPlugins(encryptPlugin);
return sessionFactory.getObject();
}
3、使用分页
//分页参数
PageHelper.startPage(page, size);
//查询mapper
List<xxxx> list = xxxxMapper.xxxx(param);
//获取分页数据
PageInfo<xxxx> info = new PageInfo<>(list);
List<xxxx> pageList = info.getList();
//处理返回信息
PageVo<xxxx> pageVo = new PageVo<>();
pageVo.setNumber(page);
pageVo.setSize(size);
pageVo.setTotalElements(info.getTotal());
pageVo.setTotalPages(info.getPageNum());
pageVo.setResultList(listRepVos);