SQL > execute SQL; SQL > execute SQL;
Sql source
The source of SQL from the last step of the previous article, the source of the main process is as follows:
You can see that BoundSql was retrieved directly from BoundSql, and then you can work backwards to find that it was returned from the getBoundSql method in MappedStatement. The MappedStatement object encapsulates all the information needed to execute an SQL in Mybatis. Therefore, it is natural to obtain the MappedStatement object from this object.
The getBoundSql method of MappedStatement
MappedStatement getBoundSql ();
First, the getBoundSql method in MappedStatement on the right. This time, the SQL is changed to pass two parameters, but it has little impact on the program. BoundSql is a DynamicSqlSource object.
Create a BoundSql object. The first parameter to create a BoundSql object is context.getsQL ().
In the getBoundSql method, the first step is to initialize the context. The Bindings property has been initialized and the sqlBuilder is still an empty string. The key code is at the current interrupt point “rootsqlNode.apply (context);” This line of code.
RootSqlNode is a MixedSqlNode object. The apply method of the MixedSqlNode object is to iterate over all elements of the property contents and apply them. You can see that contents has only one element of the TextSqlNode object. So you end up with the TextSqlNode object.
The Text property of the TextSqlNode object holds SQL that has not yet been processed.
TextSqlNode processing SQL
TextSqlNode handles SQL.
On the right is the apply method for TextSqlNode. The parameter context is the context required in the previous step, which contains the request parameters. SqlBuilder is still an empty string, and apply is to concatenate sqlBuilder.
The Apply method first creates a GenericTokenParser object, which has three attributes openToken, closeToken, and handler. OpenToken and closeToken correspond to strings “${” and”} “, respectively. The handler attribute context corresponds to the passed context, and the injectionFilter attribute is the injectionFilter of TextSqlNode. Apply the second line “context.appendsql (parser.parse(text));” Parser. parse(text) is the code that generates SQL. Where text is the raw SQL not processed in TextSqlNode.
So SQL is generated in GenericTokenParser’s parse method. The key source code for the Parse method is shown on the left. The main steps are analyzed as follows:
SQL > select * from ‘${‘ where start =’ ${‘;
2, the SRC of characters from the beginning to start stitching to the builder, also is the “${” the content of the stitching on the results;
${SRC = ${SRC = ${SRC = ${SRC = ${SRC = ${SRC = ${SRC = ${SRC = ${SRC = ${SRC = ${SRC = ${SRC = ${SRC = $} And concatenate the values into the result Builder.
Start = text.indexof (openToken, offset);
} while (start > -1); That’s step 4.
4, continue to find the next “${” and concatenate. The result is a complete SQL.
In fact, the parse method of GenericTokenParser is relatively simple, which is to replace the contents between openToken and closeToken in the passed parameter text with the corresponding parameter.
conclusion
From the previous analysis, we know that an MappedStatement corresponds to an SQL. How to obtain the assembled SQL from MappedStatement is actually the sqlSource property of MappedStatement.
SqlSource actually relies on rootSqlNode. In the previous analysis, mapper files were parsed to generate SQL into nested SqlNode subclasses. See them in action today. But today is a relatively simple, after a slightly more complex look at the SQL parsing process.
Java programmer daily study notes, such as understanding the wrong welcome to exchange discussion!