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

In yesterday’s article “The implementation of paging in the System – Data Structure”, we briefly described the paging architecture in the system, the main code interface of the backend paging in the system three characteristics:

  1. The return value type isTableDataInfo
  2. Interface first line of codestartPage()
  3. Interface last sentence:getDataTable(list)

Get paging parameters

We’ve already seen the TableDataInfo data structure, which is a paginated data structure, with total, with Rows.

Today we’ll look at what logic is executed in startPage(). The startPage method is in the base class BaseController. The code is as follows:

/** * set request paging data */
    protected void startPage(a)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if(StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) { String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy()); Boolean reasonable = pageDomain.getReasonable(); PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable); }}Copy the code

Let’s do it line by line.

The first line PageDomain PageDomain = TableSupport. BuildPageRequest (); , introduced a new class PageDomain, what does this class do?

The detailed structure of PageDomain is shown in the following code.

/** ** paging data **@author ruoyi
 */
public class PageDomain
{
    /** Start index of current record */
    private Integer pageNum;

    /** Number of records per page */
    private Integer pageSize;

    /** ** */
    private String orderByColumn;

    /** The sorting direction is desc or asC */
    private String isAsc = "asc";

    /** Paging parameters rationalize */
    private Boolean reasonable = true;
Copy the code

This contains pageNum and pageSize paging parameters, as well as sorting and sorting methods. So TableSupport. BuildPageRequest () is how to get to these parameters?

The detailed code is shown below.

    public static PageDomain buildPageRequest(a)
    {
        returngetPageDomain(); }}/** * encapsulates the paging object */
    public static PageDomain getPageDomain(a)
    {
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(ServletUtils.getParameterToInt(PAGE_NUM));
        pageDomain.setPageSize(ServletUtils.getParameterToInt(PAGE_SIZE));
        pageDomain.setOrderByColumn(ServletUtils.getParameter(ORDER_BY_COLUMN));
        pageDomain.setIsAsc(ServletUtils.getParameter(IS_ASC));
        pageDomain.setReasonable(ServletUtils.getParameterToBool(REASONABLE));
        return pageDomain;
    }
Copy the code

Inside the method, a function called getPageDomain (), in getPageDomain (), get request parameters when using a tool class methods: ServletUtils. GetParameterToInt, we take a look at this way.

public class ServletUtils
{
    /** * gets the String argument */
    public static String getParameter(String name)
    {
        return getRequest().getParameter(name);
    }
    // Other methods....
}
Copy the code

How to get the front end Request paging parameters layer by layer into the object pageDomain.

How do paging parameters translate into SQL execution?

The code for

Next, explore the startPage() method, followed by:

String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
Copy the code

Let’s look inside the method.

/** * checks for characters to prevent injection from bypassing */
    public static String escapeOrderBySql(String value)
    {
        if(StringUtils.isNotEmpty(value) && ! isValidOrderBySql(value)) {throw new UtilException("Parameters do not meet specifications, cannot be queried");
        }
        return value;
    }
Copy the code

This method checks if the ORDER_BY SQL statement is valid.

Next, we come to the key: Pagehelper.startPage (pageNum, pageSize, orderBy).setreasonable (reasonable); , where the startPage method, let’s take a look at its execution code:

/** Abstract class: */ in PageMethod
public static <E> com.github.pagehelper.Page<E> startPage(int pageNum, int pageSize, java.lang.String orderBy) {
    return null;
  }
Copy the code

A face meng force!

Oh ~ after asking Baidu, we know, this is a pagination tool related to Mybatis, called PageHelper, I don’t know.

We also found the configuration in pom.xml:

<! -- PageHelper pagination plugin -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
        </dependency>
Copy the code

In the configuration file application.yml:

PageHelper: helperDialect: mysql supportMethodsArguments: True Params: Count =countSqlCopy the code