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

In an article a few days ago, we introduced the pagination method for PageHelper, which reads code to target executorUtil.pagequery (…). Method, and read some of the code.

Today we will look at the important SQL modification code.

getPageSql

Let’s look at the code:

if(! dialect.beforePage(ms, parameter, rowBounds)) {return executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql);
} else {
    parameter = dialect.processParameterObject(ms, parameter, boundSql, cacheKey);
    String pageSql = dialect.getPageSql(ms, boundSql, parameter, rowBounds, cacheKey);
    // Other code...
}
Copy the code

You should read the dialect.getPagesQL method today. MySql > implement getPageSql from Dialect to MysqlDialect

public class MySqlDialect extends AbstractHelperDialect {
    public String getPageSql(String sql, Page page, CacheKey pageKey) {
        StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14);
        sqlBuilder.append(sql);
        if (page.getStartRow() == 0L) {
            sqlBuilder.append("\n LIMIT ? ");
        } else {
            sqlBuilder.append("\n LIMIT ?, ? ");
        }

        returnsqlBuilder.toString(); }}Copy the code

After much searching, we finally see a direct change to the SQL statement. (^ del ^).

The syntax here is also worth learning. First, use StringBuilder to speed up string concatenation. New StringBulder(sql.length() + 14); new StringBulder(sql.length() + 14);

Then we see SQL concatenation!!

First check if it is the first page. If it is the first page, concatenate \n LIMIT? , not the first page splice \n LIMIT? ,? .

At this point, we finally see the true face of SQL modification.

In honor of the table, having established that we’re looking at the code execution location, today we’ll take a different approach and use IDEA to step by step look at Java code execution.

If a page in the system, such as “System Monitoring -> Scheduled Task” has paging, we take this as an example.

When the page is loaded, it looks like this:

We start by breaking a breakpoint on the PageInterceptor interceptor entrance. :

And successfully intercepted.

Then we will create a breakpoint from the MysqlDialect change method and successfully intercept again! Happy!