What does Mybatis dynamic sql do? What kind of dynamic sql are there? Can you briefly describe the execution principle of dynamic sql?

        OGNL expression

        OGNL, the full name of Object-Graph Navigation Language, is a powerful expression language used to obtain and set the properties of Java objects. It aims to provide a higher and more abstract level to navigate Java object graphs.

        The basic unit of OGNL expression is "navigation chain". Generally, the navigation chain consists of the following parts:

Property name (property) 
Method invoke (method invoke) 
Array element
All OGNL expressions are evaluated based on the context of the current object. The results of the previous part of the chain will be used as the context for subsequent evaluations. For example: names[0].length().

        MyBatis dynamic SQL uses OGNL expressions to determine conditions and determine whether to include a certain SQL fragment based on the conditions. OGNL expressions are a Java expression-based language that provides access to Java objects and methods. Dynamic SQL is a technology that allows specific query conditions, table names, column names, etc. to be dynamically generated in SQL statements based on different conditions. By using dynamic SQL, you can reduce code duplication and improve code flexibility and maintainability.

        Mybatis provides the following commonly used dynamic SQL tags:

if、choose、when、otherwise、trim、where、set、foreach、bind、include

1、<if>: 

if is to determine whether the incoming value conforms to certain rules, such as whether it is not empty;

2、<where>:

 The where tag can be used to make dynamic splicing query conditions. The where element will only insert the "WHERE" clause if the child element returns any content. If the beginning of the clause is "AND" or "OR", the where element will also insert Remove them.

3、<choose><when><otherwise>:

This is a set of combination tags, their functions are similar to switch, case, and default in Java. Only one condition takes effect, that is, only the satisfied condition when is executed. If the condition is not satisfied, otherwise is executed, indicating the default condition;

4、<foreach>:

The foreach tag can traverse the incoming collection object, and then pass the content of each item as a parameter to the sql statement, which involves item (specific each object), index (serial number), open (start symbol), close(terminator), separator(separator);

u

6. <set>:
Applicable to modification and update. When a certain condition is matched, the field will be updated.

7. <trim>:
The trim tag is usually used to remove specific characters or keywords.
It is a formatting tag with 4 main parameters:

    prefix(prefix);

    prefixOverrides(remove the first mark);

    suffix(suffix);

    suffixOverrides(remove the last mark);

8. <bind>: 
The bind tag is used to define a variable, which can be referenced by subsequent SQL fragments, which facilitates the writing of SQL.


        The execution principle of dynamic sql:
Part 1: When starting the SqlSessionFactory to load and parse the xml configuration file, it is parsed, encapsulated into the corresponding handler processing object according to the key tags, and encapsulated into the sqlSource object with mappedStatement.

Calling process:

        1. When SqlSessionFactoryBuilder calls the builder object, it calls XMLConfigBuilder to parse the sqlMapConfig.xml configuration file, and uses the private mapperElement (XNode parent) method during the parsing process.

        2. In the above method, all configuration mapper configurations are obtained by building XMLMapperBuilder, calling the private void configurationElement(XNode context) method to parse mapper.xml, and using the void buildStatementFromContext(List<XNode> list, String requiredDatabaseId) method to parse the mapper. Each tag in xml

        3. Build the XMLStatementBuilder object in the loop and call the parseStatementNode() method to encapsulate the mappedStatment object.

        4. In the process, the sqlSource object needs to be constructed, processed through the XMLLanguageDriver object, and the XMLScriptBuilder, the parsing dynamic tag object, is built in the XMLLanguageDriver.

Part 2: When obtaining the bondSql object from sqlSource during execution, execute the corresponding label handler.

        When calling the query method of BaseExecutor, it will go to getBoundSql and pass the parameters in. In the sqlSource interface DynamicSqlSource implementation class, the getBoundSql method is called during the execution process to create a DynamicContext object for judgment, analysis, and encapsulation into a SqlSource object for return.

Guess you like

Origin blog.csdn.net/weixin_67224308/article/details/131931040