MyBatis分页插件PageHelper


官方文档:https://pagehelper.github.io/docs/

我们在  的基础上使用分页插件。

引入分页插件 


    com.github.pagehelper
    pagehelper
    5.1.11

配置拦截器插件

特别注意,新版拦截器是 com.github.pagehelper.PageInterceptor。 com.github.pagehelper.PageHelper 现在是一个特殊的 dialect 实现类,是分页插件的默认实现类,提供了和以前相同的用法。

① 在 MyBatis 配置 xml 中配置拦截器插件



    
    
        
        
	

② 在 Spring 配置文件中配置拦截器插件

使用 spring 的属性配置方式,可以使用 plugins 属性像下面这样配置:


  
  
    
      
        
          
          
            params=value1
          
        
      
    
  

如何在代码中使用

mapper接口:

List listAll();

mapper xml:

测试代码:

//第一种方式:
//第几页,每页几条数据
Page page = PageHelper.startPage(1, 2);
//紧跟着的第一个select方法会被分页
List userList = userMapper.listAll();
System.out.println("count:" + page.getTotal());
System.out.println("user list :" + userList);

//第二种方式:
//PageHelper.startPage(1, 2);
//List userList = userMapper.listAll();
//PageInfo pageInfo = new PageInfo(userList);
//System.out.println("count:" + pageInfo.getTotal());
//System.out.println("user list :" + userList);