MyBitis automatically spliced LIMIT

1 Introduction

Recently, a very strange problem was discovered during the operation of the system. Inexplicable SQL statements will be spliced ​​into a small piece of SQL. However, it is found that the spliced ​​SQL is not the SQL currently used by this API, thus causing an error in the select statement.

2.Troubleshooting ideas

2.1. The first step

First, I checked the Mapper corresponding to the error in the print log and found that this dynamic splicing SQL cannot be generated by this Mapper under normal circumstances.

After finding the automatically spliced ​​SQL, I first searched the full text for "order by a.createTime desc" and found that no function used this statement. The structure "order by xxxxx limit" looked a bit like it was automatically spliced ​​by the paging plug-in PageHelper. of.

2.2. The second step

Then I narrowed the scope of the query and searched for "a.createTime desc", and found that there were indeed several methods used. After checking one by one, I found that one of the methods caught my attention.

The paging plug-in PageHelper is used here, and orderBy is used for sorting, which is more in line with the current automatic splicing phenomenon. But why is this splicing used by other Mapper? Here we need to understand the underlying implementation of PageHelper.

Looking at our code, we have initialized PageHelper before if. At this time, the paging plug-in LOCAL_PAGE.set(page); has been assigned, but if our if does not meet the requirements, it will jump directly to else and return null. Although PageHelper has been assigned a value throughout the process, it has not been used and therefore has not been destroyed. Therefore, when we execute the next select statement, it may be spliced ​​by this statement, causing query errors.

3.Problem handling

Now that we have located the cause of the problem, it is easy to troubleshoot. We can deal with this problem in two ways.

The first is to initialize the paging plug-in in the if method block, that is:

Second, manually clean up once in the else method block, that is:

Obviously, the first method is better, and initialize it when used.

Guess you like

Origin blog.csdn.net/qq_17486399/article/details/134159589