目录

mybatis插件开发值PageHelper

PageHelper插件官网

PageHelper中文文档

使用方法

示例程序

数据准备

https://gitee.com/lienhui68/picStore/raw/master/null/20201011152954.png

引入maven依赖

1
2
3
4
5
6
<!-- 分页插件 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.2.0</version>
</dependency>

测试程序

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
@Test
public void test5() throws IOException {
    SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
    try (SqlSession session = sqlSessionFactory.openSession(true)
    ) {
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        // 设置显示第二页内容,每页大小为2
        PageHelper.startPage(2, 2);
        List<Employee> employees = employeeMapper.getAllEmployees();
        System.out.println(employees);
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
20201011 15:28:56 [main] DEBUG net.sf.ehcache.Cache - Initialised cache: com.eh.eden.mybatis.orm.dao.EmployeeMapper
20201011 15:28:56 [main] DEBUG net.sf.ehcache.config.ConfigurationHelper - CacheDecoratorFactory not configured for defaultCache. Skipping for 'com.eh.eden.mybatis.orm.dao.EmployeeMapper'.
20201011 15:28:56 [main] DEBUG o.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 1258372214.
20201011 15:28:56 [main] DEBUG o.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 1258372214 to pool.
20201011 15:28:56 [main] DEBUG com.eh.eden.mybatis.orm.dao.EmployeeMapper - Cache Hit Ratio [com.eh.eden.mybatis.orm.dao.EmployeeMapper]: 0.0
20201011 15:28:56 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
20201011 15:28:56 [main] DEBUG o.apache.ibatis.datasource.pooled.PooledDataSource - Checked out connection 1258372214 from pool.
20201011 15:28:56 [main] DEBUG c.e.e.m.o.dao.EmployeeMapper.getAllEmployees_COUNT - ==>  Preparing: SELECT count(0) FROM tbl_employee
20201011 15:28:56 [main] DEBUG c.e.e.m.o.dao.EmployeeMapper.getAllEmployees_COUNT - ==> Parameters: 
20201011 15:28:56 [main] TRACE c.e.e.m.o.dao.EmployeeMapper.getAllEmployees_COUNT - <==    Columns: count(0)
20201011 15:28:56 [main] TRACE c.e.e.m.o.dao.EmployeeMapper.getAllEmployees_COUNT - <==        Row: 6
20201011 15:28:56 [main] DEBUG c.e.e.m.o.dao.EmployeeMapper.getAllEmployees_COUNT - <==      Total: 1
20201011 15:28:56 [main] DEBUG com.eh.eden.mybatis.orm.dao.EmployeeMapper - Cache Hit Ratio [com.eh.eden.mybatis.orm.dao.EmployeeMapper]: 0.0
20201011 15:28:56 [main] DEBUG c.e.e.m.orm.dao.EmployeeMapper.getAllEmployees - ==>  Preparing: select * from tbl_employee LIMIT ?, ?
20201011 15:28:56 [main] DEBUG c.e.e.m.orm.dao.EmployeeMapper.getAllEmployees - ==> Parameters: 2(Long), 2(Integer)
20201011 15:28:56 [main] TRACE c.e.e.m.orm.dao.EmployeeMapper.getAllEmployees - <==    Columns: id, last_name, gender, email, dept_id
20201011 15:28:56 [main] TRACE c.e.e.m.orm.dao.EmployeeMapper.getAllEmployees - <==        Row: 9, 宋江, 1, sj@sh.com, 1
20201011 15:28:56 [main] TRACE c.e.e.m.orm.dao.EmployeeMapper.getAllEmployees - <==        Row: 10, 武松, 1, ws@sh.com, 2
20201011 15:28:56 [main] DEBUG c.e.e.m.orm.dao.EmployeeMapper.getAllEmployees - <==      Total: 2
Page{count=true, pageNum=2, pageSize=2, startRow=2, endRow=4, total=6, pages=3, reasonable=false, pageSizeZero=false}[Employee(id=9, lastName=null, gender=1, email=sj@sh.com, dept=null), Employee(id=10, lastName=null, gender=1, email=ws@sh.com, dept=null)]

传入要连续显示多少页

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Test
 public void test5() throws IOException {
     SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
     try (SqlSession session = sqlSessionFactory.openSession(true)
     ) {
         EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
         Page<Object> page = PageHelper.startPage(5, 1);

         List<Employee> employees = employeeMapper.getAllEmployees();
         //传入要连续显示多少页
         PageInfo<Employee> info = new PageInfo<>(employees, 5);
         for (Employee employee : employees) {
             System.out.println(employee);
         }
        /*System.out.println("当前页码:"+page.getPageNum());
        System.out.println("总记录数:"+page.getTotal());
        System.out.println("每页的记录数:"+page.getPageSize());
        System.out.println("总页码:"+page.getPages());*/
         ///xxx
         System.out.println("当前页码:" + info.getPageNum());
         System.out.println("总记录数:" + info.getTotal());
         System.out.println("每页的记录数:" + info.getPageSize());
         System.out.println("总页码:" + info.getPages());
         System.out.println("是否第一页:" + info.isIsFirstPage());
         System.out.println("连续显示的页码:");
         int[] nums = info.getNavigatepageNums();
         for (int i = 0; i < nums.length; i++) {
             System.out.println(nums[i]);
         }
     }
 }