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

There is a Dialect in PageInterceptor that can be configured to implement classes in java.util.properties to adapt to different database forms, such as mysql, Oracle, etc.

Today, we follow up on the Query method from ExecutorUtil, which we looked at last time.

Dialect we have already known that the dialect would be MySqlDialect in our system if it is mysql. Let’s see in turn where is the implementation of the call in the paging method?

public static <E> List<E> pageQuery(Dialect dialect, Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql, CacheKey cacheKey) throws SQLException {
        // Determine whether paging queries are required
        if (dialect.beforePage(ms, parameter, rowBounds)) {
            //.... 
        }
Copy the code

Select Dialect.beforePage from MySqlDialect where the dialect.dialect.implement () method is used to determine whether pagination is required.

The MySqlDialect hierarchy

MySqlDialect = MySqlDialect

Dialect -> AbstractDialect -> AbstractHelperDialect -> MySqlDialect

From the annotation of each method we can see:

  1. Dialect: database Dialect implemented for different databases.
  2. AbstractDialect: Intelligent Count queries based on CountSqlParser;
  3. Abstract class AbstractHelperDialect: implementation of PageHelper;
  4. Implementation class MySqlDialect: Specific implementation for the mysql database.

Where method beforePage(…) Where are the definitions and implementations of? Since it is called in Dialect, we know that the definition must be in the highest-level Dialect interface, so where is the implementation?

Search layer by layer, beforePage(…) It is in the third layer class AbstractHelperDialect. The code is as follows:

@Override
public boolean beforePage(MappedStatement ms, Object parameterObject, RowBounds rowBounds) {
    Page page = getLocalPage();
    if (page.isOrderByOnly() || page.getPageSize() > 0) {
        return true;
    }
    return false;
}
Copy the code

SQL > pageSize (isOrderByOnly); SQL > pageSize (isOrderByOnly); SQL > pageSize (isOrderByOnly);

Several variables and comments for Page are as follows:

public class Page<E> extends ArrayList<E> implements Closeable {
    private static final long serialVersionUID = 1L;
    private int pageNum;
    private int pageSize;
    private long startRow;
    private long endRow;
    private long total;
    /** * Total pages */
    private int pages;
    /** * contains the count query */
    private boolean count = true;
    /** ** ** */
    private Boolean reasonable;
    /** * When set to true, if pagesize is set to 0 (or RowBounds limit=0), no paging is performed and all results are returned */
    private           Boolean                   pageSizeZero;
    /** * The column name for the count query */
    private           String                    countColumn;
    /** ** sort */
    private           String                    orderBy;
    /** * only adds sort */
    private           boolean                   orderByOnly;
Copy the code

When is the Page parameter set? What logic is used to set orderByOnly and pageSize, and how do we get the Page object at the time of the SQL execution here?