Use the pageHelper plug-in in spring-boot

Add the dependent

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>

</dependency>
Copy the code

Note: This is the dependency if you are an MVC project

<! -- PageHelper pagination plugin -->
<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper</artifactId>
  <version>4.2.0</version>
</dependency>
Copy the code

An error is raised if a Spring-MVC project is introduced in a spring-boot project

Paging method

@RequestMapping("/findAll")
// @requestparam (required = true,defaultValue = "1") if the foreground page does not pass in a page value, then the defaultValue is 1
public String findAll(@RequestParam(required = true,defaultValue = "1") Integer page, Model model){
    // Start paging
    PageHelper.startPage(page,10);
    List<Car> carList = autoService.findAll();
    PageInfo<Car> pageInfo = new PageInfo<>(carList);
    model.addAttribute("list",carList);
    // Store the paging information in the Model object for use by the foreground object
    model.addAttribute("page",pageInfo);
    return "list";
}
Copy the code

The front desk page

<p>The first page ${page. PageNum}</p>
<p>A total of ${page. Pages} page</p>
<div><a href="findAll? page=${page.pages-(page.pages-1)}" class="layui-btn">Home page</a></div>
<div><a href="findAll? page=${page.prePage}" class="layui-btn">The previous page</a></div>
<div><a href="findAll? page=${page.nextPage}" class="layui-btn">The next page</a></div>
<div><a href="findAll? page=${page.pages}" class="layui-btn">back</a></div>
Copy the code

Common PageInfo object methods

Pages: total number of pages

PageNum: the current page

PrePage: back

NextPage: the next page

FirstPage: the first page

LastPage: lastPage

But it’s important to note that firstPage is the firstPage retrieved from the navigation bar, and lastPage is the lastPage retrieved from the navigation bar, not really the lastPage

By observing the source code:

@Deprecated
// firstPage is 1. This function retrieves the firstPage of the navigation bar
public int getFirstPage(a) {
    return navigateFirstPage;
}
 @Deprecated
    // Use getPages() to get the last page. This function gets the last page of the navigation bar.
    public int getLastPage(a) {
        return navigateLastPage;
    }

Copy the code

Solutions:

Through the foreground code observation can be concluded that click the button of the last page is page=” the value of the last page “, passed to the background, the background to query

So the last page can also be written as Pages

Page. Pages -(page. Pages -1) Total pages minus total pages minus one

Principles of paging

Paging works in

Advanced query (three) : paging query:

See this article for more details

Aop, which works like Spring, intercepts SQL statements for enhancement

For example, SQL :select * from tableName

SQL: select * from tableName limit? ,?