1. Add dependencies
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
2. In the main configuration file of MyBatis, configure the paging plugin
<!--以下是简洁配置,一般只需要按这个方式进行设置即可-->
<plugins>
<!--设置分页插件-->
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
3. Simple application of pagination plug-in
Use PageHelper.startPage(int pageNum, int pageSize) to enable paging function before query function
@Test
public void pageSearch(){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
System.out.println(userMapper);
//简单使用
Page<User> page = PageHelper.startPage(5,5);
List<User> list = userMapper.selectUser();
System.out.println(page);
for (User user : list) {
System.out.println(user);
}
}
4. Paging query detailed information acquisition
After querying to get the list collection , use
PageInfo<T> pageInfo = new PageInfo<>(list<T> list,int navigatePages) to get pagination related data
- list: data after pagination
- navigatePages: the number of pages in the navigation page
@Test
public void pageSearch(){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
System.out.println(userMapper);
Page<User> page = PageHelper.startPage(8,3);
List<User> list = userMapper.selectUser();
//详细的分页信息
PageInfo<User> pageInfo = new PageInfo<>(list,7);
System.out.println(pageInfo);
for (User user : list) {
System.out.println(user);
}
}
Paging related data/common data:
pageNum: the page number of the current page
pageSize: the number of items displayed on each page
size: the actual number of items displayed on the current page
total: the total number of records
pages: total number of pages
prePage: the page number of the previous page
nextPage: the page number of the next page
isFirstPage/isLastPage: whether it is the first page/last page
hasPreviousPage/hasNextPage: Whether there is a previous page/next page
navigatePages: the number of pages in the navigation page
navigatepageNums: the page number of the navigation page, [1,2,3,4,5]