Mybatis study notes -09 paging

Due to the large amount of data queried in modern databases, we often use paging methods, which can better display the data, and avoid slow response due to too many data queries at a time. Mybatis provides PageHelper plug-in for paging query.

Basic use

1. Import dependencies

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

2. Add a plug-in to the main configuration file. Use the plug-in tag <plugin> (note the location of the tag)

(properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, reflectorFactory?, plugins?, environments?, databaseIdProvider?, mappers?)
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>

3. Test the paging function

    @Test
    public void testPage(){
        PageHelper.startPage(1,2);
        List<User> users = mapper.queryUsers();
        for (User user : users) {
            System.out.println(user);
        }
        System.out.println("========================");
        users = mapper.queryUsers();
        for (User user : users) {
            System.out.println(user);
        }
    }

[Note]: 1. Pagination query is realized by setting PageHelper.start (query the first few pages [page number starts from 1], several data in a page), which is only valid for the first query afterwards.

         2. In fact, what this paging plug-in helps us do is to append LIMIT after SQL, and the bottom layer is still implemented using native SQL.

         3. The paging plugin does not support query statements with exclusive lock "for update".

         4. The paging plug-in does not support "nested queries" and may not get the correct number of results.

Advanced operation

Mybatis's paging plug-in also provides the PageInfo class, which has many attributes to facilitate advanced development.

Detailed attribute:


private int pageNum : 当前页 
private int pageSize : 每页的数量   
private int size : 当前页的数量(因为最后一页可能没填满,如11条记录,每页3条,最后一个只有2条)
private int startRow : 当前页面第一个元素在数据库中的行号  
private int endRow : 当前页面最后一个元素在数据库中的行号 
private int pages : 总页数
private int prePage : 前一页的页号,没有则为0 
private int nextPage : 后一页的页号,没有则为0 
private boolean isFirstPage :  是否为第一页
private boolean isLastPage = false : 是否为最后一页  
private long total : 总记录数
private boolean hasPreviousPage = false : 是否有前一页  
private boolean hasNextPage = false : 是否有下一页
private int navigatePages : 每一页所有的导航页码的数量
private int[] navigatepageNums : 存储所有页号的数组
private int navigateFirstPage : 第一页的页号
private int navigateLastPage : 最后一页的页号
private int total : 总的页数


[Note]: The navigatePages attribute may be more difficult to understand. It refers to the number of tabs 1, 2,,,,, 10 in the figure below, that is, the number of navigation pages displayed on each page.

Guess you like

Origin blog.csdn.net/qq_39304630/article/details/112296880