1. Explanation
If you are using Mybatis, it is recommended to try this paging plugin, which is definitely the most convenient paging plugin.
This plug-in currently supports six database paging: Oracle, Mysql, MariaDB, SQLite, Hsqldb, and PostgreSQL.
2. How to use
Step 1: Configure the interceptor plugin in Mybatis configuration xml:
<plugins> <!-- com.github.pagehelper is the package name where the PageHelper class is located --> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- Set database types Oracle, Mysql, MariaDB, SQLite, Hsqldb, PostgreSQL six databases --> <property name="dialect" value="mysql"/> </plugin></plugins>
Step 2: Use in the code
1. Set paging information:
//Get page 1, 10 items, default query total countPageHelper.startPage(1, 10); //The first select method immediately following will be paginated List<Country> list = countryMapper.selectIf(1);
2. Get pagination information
//After paging, the actual returned result list type is Page<E>. If you want to get the paging information, you need to cast it to Page<E>, Page<Country> listCountry = (Page<Country>)list; listCountry.getTotal();
3. The second method to get paging information
//Get the 1st page, 10 items, the default total number of queries countPageHelper.startPage(1, 10); List<Country> list = countryMapper.selectAll();//Wrap the result with PageInfo PageInfo page = new PageInfo(list);//Test all properties of PageInfo//PageInfo contains very comprehensive pagination attributes assertEquals(1, page.getPageNum()); assertEquals(10, page.getPageSize()); assertEquals(1, page.getStartRow()); 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()); assertEquals(true, page.isHasNextPage());