This is the 13th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

How do I paginate in my daily work? We can splicing limits in SQL, but this is not a little low, so I visited the great Baidu, and I know a plug-in pageHelper, so let’s introduce PageHelper.

Integration of 1.

1. Add the POM file

First add the dependencies to the POM file. For the same version, you can visit the official website. Pagehelper.github. IO/There is also an official document inside, if you want to study it carefully, you can go to see it.

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

2. Writing the controller

If PageNum and PageSize are not directly queried, use page = pagehelper.startPage () pagination query directly. You can also use Long total = page.gettotal (); Gets the total number of entries.

public Result<*>(*VO *VO) {
        Result<*> resultVO = new Result<>();
        / / paging
        Page page = new Page();
        if(! CommonUtil.isEmpty(*VO.getPageNum()) && ! CommonUtil.isEmpty(*VO.getPageSize())) { page = PageHelper.startPage(*VO.getPageNum(), *VO.getPageSize()); resultVO.setPageNum(*VO.getPageNum()); resultVO.setPageSize(*VO.getPageSize()); }// ASC is the forward sort by ID, DESC is the reverse sort
        if(! CommonUtil.isEmpty(*VO.getOrder())) { PageHelper.orderBy(*VO.getOrder()); }// Business query only this sentence business query!!
        List<*VO> result = *Service.query(*VO);
        // Total page count encapsulation
        Long total = page.getTotal();
        resultVO.setTotal(total);
        // Entity encapsulation
        resultVO.setData(result );
        return resultVO;
    }
Copy the code

If there are two queries in the Controller, then the second query will have the condition of the first query. If this is the case, you can create services and call them separately.

3. The service layer

Very simple, no what to say, ordinary add, delete, change, check can not be shown here. #

4. The configuration class

You need to add the following configuration to the configuration class, which varies according to the database type.

pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql
Copy the code

By the way, PageHelper is a physical pagination method (concatenating liimit to SQL). It queries the data directly in the database, rather than all the data, and then intercepts it again in memory, so it is more efficient.