mybatis 分页插件的使用
1、导入依赖
<!--mybatis分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
2、在 mybatis 配置文件中配置插件
<!--配置分页插件-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--默认值是false,当查询的页码大于最大页码,不显示内容
设置为true,当查询的页码大于最大页码,显示最后一页-->
<property name="reasonable" value="true"/>
</plugin>
</plugins>
3、在service中使用 Page 封装返回值
传入当前页面编号和页面大小,开启分页,将 需要返回的结果 封装到 Page 对象中,分页插件会根据我们使用的数据库类型,自动的获取分页信息,将其封装到page对象中。
/**
* 分页查询所有角色
* @param pageNum 要查询的页面编号
* @param pageSize 页面大小
* @return page对象,封装了返回数据和页面的信息
*/
public Page<Role> findAllRoleByPaging(Integer pageNum, Integer pageSize) {
//设置分页信息
PageHelper.startPage(pageNum, pageSize);
Page<Role> page = (Page<Role>) roleMapper.findAllRole();
System.out.println(page.getPageNum());
System.out.println(page.getPages());
System.out.println(page.getResult());
return page;
}