Mybatis pagehelper

Mybatis pagehelper

2018, Jan 16    

The most headache is when to use the Mybatis write paging, need to write a query count of a select statement. Then write a real paging query statements, much later, when the query conditions will find really don’t want to take double the time to write the count and the select, can be found that duplicated code is too much, although very simple copy and paste, but the length of the file in increasing exponentially, after reading the code when the head can be big.

So I want to write only a select statement, and count is done automatically by the plug-in based on the select statement. To find ah find ah, find PageHelperhttps://github.com/pagehelper/Mybatis-PageHelper meets the requirements

使用过程

1.引入pageHelper

<dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
</dependency>

2.mybatis-config注册

 <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>

3.查询之前只需要调用,传入页码,每页的大小

PageHelper.startPage(pn,5);

4.startPage后面紧跟的查询是一个分页查询

List<Employee> employeeList = iEmployeeService.getAll();

5.使用pageInfo包装查询后的结果

PageInfo pageInfo = new PageInfo(employeeList);