[Mybatis] Use of Mybatis paging plugin

paging plugin

1. Paging plug-in configuration steps
(1) Add paging plug-in dependencies in pom.xml

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

(2) Configure the paging plugin in the core configuration file mybatis-config.xml of mybatis

<plugins>
    <!--设置分页插件-->
    <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>

2. The use of paging plug-ins
Let's take a look at the format of paging queries:

SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset;

For example: paging query on the product table:

SELECT * FROM product LIMIT 0,3;
<!--分页查询时相关参数的说明:-->
limit index , pagesize
index:当前页的起始索引
pagesize:每页显示的条数
pageNum:当前页的页码
index = ( pageNum-1 ) * pagesize

3. Steps of using the paging plug-in
(1) Use PageHelper.startPage(int pageNum, int pageSize) to enable the paging function before the query function
pageNum: the page number of the current page
pagesize: the number of items displayed on each page
(2) After querying to obtain the list collection , use PageInfo pageInfo = new PageInfo<>(List list, int navigatePages) to get paging-related data
list: data after paging
navigatePages: page number of navigation paging (usually use odd numbers)
insert image description here
4. Test method:
test on the basis of reverse engineering Pagination plugin.

public class PageHelperTest {
    
    
    @Test
    public void Test01(){
    
    
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
//        Page<Object> page = PageHelper.startPage(1, 5);
        //查询所有数据
        PageHelper.startPage(1, 5);
        List<Emp> list = mapper.selectByExample(null);
        PageInfo<Emp> page = new PageInfo<Emp>(list, 5);
        System.out.println(page);
    }
}

search result:
insert image description here

PageInfo{pageNum=1, pageSize=5, size=5, startRow=1, endRow=5, total=8, pages=2, 
list=Page{count=true, pageNum=1, pageSize=5, startRow=0, endRow=5, total=8, pages=2, reasonable=false, pageSizeZero=false}[Emp{eid=1, empName='张三', age=2, sex='男', email='[email protected]', did=1}, Emp{eid=2, empName='李四', age=5, sex='女', email='[email protected]', did=2}, Emp{eid=3, empName='王五', age=3, sex='女', email='[email protected]', did=3}, Emp{eid=4, empName='赵六', age=5, sex='男', email='[email protected]', did=3}, Emp{eid=5, empName='田七', age=6, sex='女', email='[email protected]', did=3}],
prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=5, navigateFirstPage=1, navigateLastPage=2, navigatepageNums=[1, 2]}

Guess you like

Origin blog.csdn.net/weixin_46081857/article/details/123398778