The use of Mybatis paging plug in PageHelper

  • 2020-05-19 04:59:05
  • OfStack


If you are also using Mybatis, it is recommended to try the paging plug-in. This one is definitely the easiest to use.

The plug-in support Oracle, currently Mysql MariaDB, SQLite, Hsqldb, PostgreSQL6 database pagination.

2. Method of use

Step 1: configure the interceptor plug-in in Mybatis configuration xml:

  <!-- com.github.pagehelper for PageHelper The package name of the class  -->
  <plugin interceptor="com.github.pagehelper.PageHelper">
    <!--  Set the database type  Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL6 A database -->    
    <property name="dialect" value="mysql"/>

Step 2: use it in your code

1. Set paging information:

// For the first 1 Page, 10 Bar content, default query total count
PageHelper.startPage(1, 10);
 // Next to the first 1 a select Methods are paged 
List<Country> list = countryMapper.selectIf(1);

2. Fetch paging information

// After paging, the actual result is returned list Type is Page<E> If you want to extract the paging information, you need to cast to Page<E> . 
Page<Country> listCountry = (Page<Country>)list;

3, the second way to get paging information

// For the first 1 Page, 10 Bar content, default query total count
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectAll();
// with PageInfo Wrap the results 
PageInfo page = new PageInfo(list);
// test PageInfo All properties 
//PageInfo Contains very comprehensive paging properties 
assertEquals(1, page.getPageNum());
assertEquals(10, page.getPageSize());
assertEquals(1, page.getStartRow());
assertEquals(10, page.getEndRow());
assertEquals(183, page.getTotal());
assertEquals(19, page.getPages());
assertEquals(1, page.getFirstPage());
assertEquals(8, page.getLastPage());
assertEquals(true, page.isFirstPage());
assertEquals(false, page.isLastPage());
assertEquals(false, page.isHasPreviousPage());
assertEquals(true, page.isHasNextPage());

Related articles: