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

Mysql > Create PageHelper class from Dialect to MySqlDialect. Mysql > create PageHelper class from Dialect to MySqlDialect.

Dialect -> AbstractDialect -> AbstractHelperDialect -> MySqlDialect

BeforePage method definition and Dialect. In AbstractHelperDialect, determine whether SQL statements need to be paginated.

There is a Page object involved, how is the Page object fetched in the beforePage? Let’s take a look today.

Page to obtain

Let’s look at how the Page object is retrieved in the beforePage:

Page page = getLocalPage();
Copy the code

Let’s look at the getLocalPage method again:

    /** * get the paging parameter *@param <T>
     * @return* /
    public <T> Page<T> getLocalPage(a) {
        return PageHelper.getLocalPage();
    }
Copy the code

Well, take a closer look:

    /** * Get the Page parameter *@return* /
    public static <T> Page<T> getLocalPage(a) {
        return LOCAL_PAGE.get();
    }
Copy the code

How is LOCAL_PAGE defined here?

/** * Base paging method *@author liuzh
 */
public abstract class PageMethod {
    protected static final ThreadLocal<Page> LOCAL_PAGE = new ThreadLocal<Page>();

    /** * Set Page parameter *@param page
     */
    protected static void setLocalPage(Page page) { LOCAL_PAGE.set(page); }}Copy the code

Does this LOCAL_PAGE sound familiar?

The startPage method in PageMethod is called through the Controller method startPage in the call hierarchy:

 public static <E> Page<E> startPage(int pageNum, int pageSize, boolean count, Boolean reasonable, Boolean pageSizeZero) {
        Page<E> page = new Page<E>(pageNum, pageSize, count);
        page.setReasonable(reasonable);
        page.setPageSizeZero(pageSizeZero);
        // When Order Derby has already been executed
        Page<E> oldPage = getLocalPage();
        if(oldPage ! =null && oldPage.isOrderByOnly()) {
            page.setOrderBy(oldPage.getOrderBy());
        }
        setLocalPage(page);
        return page;
    }
    
    /** * Set Page parameter **@param page
     */
    protected static void setLocalPage(Page page) {
        LOCAL_PAGE.set(page);
    }
Copy the code

This passes the parameters through an internal thread variable ThreadLocal .

Well, this way of passing ginseng can not be done without in-depth understanding of the skill! Anyway, I dare not use ~~

We then look at the judgment logic in the beforePage method:

@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

If the paging parameter orderByOnly is true or has pageSize greater than 0, paging is performed.

Let’s go back to the logic in the pageQuery 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)) {
            // Generate a cache key for paging
            CacheKey pageKey = cacheKey;
            // Process the parameter objectparameter = dialect.processParameterObject(ms, parameter, boundSql, pageKey); .Copy the code

We won’t talk about caching for the moment.

Dialect method processParameterObject (dialect method processParameterObject).dialect method processParameterObject (Dialect method processParameterObject)

It is found in the same class AbstractHelperDialect as beforePage.

We won’t go into the processParameterObject method in order to quickly see where the SQL changes are.