Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Source Code Analysis of executor packet sentence processing function of mybatis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

The knowledge of this article "mybatis's executor package sentence processing function source code analysis" is not understood by most people, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "mybatis executor package sentence processing function source code analysis" article.

1.mybatis support for multiple statement types

Pass parameters in the mybatis mapping file, mainly using # {} or ${}.

# {}: indicates that variables that use this symbol are assigned to the sql fragment in precompiled form.

${}: indicates that variables that use this symbol are inserted directly into the sql fragment as a string.

Three statement types are supported in mybatis, and different statement types support different variable symbols. The three types of mybatis are as follows:

STATEMENT: in this type of statement, only simple string concatenation is performed on sql fragments. Only ${} is supported.

PREPARED: in this statement, the sql fragment is concatenated with strings and then assigned to the sql fragment. You can use # {} and ${}.

CALLABLE: this statement uses a call to implement the execution process, first concatenating the sql fragment as a string, and then assigning a value to the sql fragment. You can use # {} and ${}.

Statement processing function of 2.mybatis

The statement subpackage is responsible for providing statement processing function, in which StatementHandler is the parent interface of the statement function class, and the RoutingStatementHandler class is a proxy class, which can select a specific proxied object according to the specific type of the incoming MappedStatement object, and then delegate all the actual operations to the proxied object. So the RoutingStatementHandler class provides routing capabilities, and routing is based on the statement type.

Public class RoutingStatementHandler implements StatementHandler {/ / private final StatementHandler delegate; public RoutingStatementHandler (Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {/ / Select the delegated object switch (ms.getStatementType ()) {case STATEMENT: delegate = new SimpleStatementHandler (executor, ms, parameter, rowBounds, resultHandler, boundSql) according to the statement type; break Case PREPARED: delegate = new PreparedStatementHandler (executor, ms, parameter, rowBounds, resultHandler, boundSql); break; case CALLABLE: delegate = new CallableStatementHandler (executor, ms, parameter, rowBounds, resultHandler, boundSql); break; default: throw new ExecutorException ("Unknown statement type:" + ms.getStatementType ());}

BaseStatementHandler, as the parent of the three implementation classes, provides common methods for the implementation class. And the template pattern used by the BaseStatementHandler class defines the framework of the entire method in the prepare method, and then leaves some operations related to the subclass to three subclasses.

The SimpleStatementHandler class, the PreparedStatementHandler class, and the CallableStatementHandler class are three true statement processors that handle statement, preparedStatement, and CallableStatement objects, respectively. Through the parameterize method, we can see the difference of the three Statement processors.

The implementation of the parameterize method in SimpleStatementHandler is empty because it only needs to complete string substitution and does not require parameter handling

Public class SimpleStatementHandler extends BaseStatementHandler {@ Override public void parameterize (Statement statement) {/ / NCMA}}

The parameterize method in PreparedStatementHandler finally invokes the parameter assignment method in the PreparedStatement class after multi-level transfer through the ParameterHandler interface.

Public class PreparedStatementHandler extends BaseStatementHandler {@ Override public void parameterize (Statement statement) throws SQLException {parameterHandler.setParameters ((PreparedStatement) statement);}}

In CallableStatementHandler, parameterize mainly calls the output parameter registration method in CallableStatement after registerOutputParameters method transfer, and then invokes the parameter assignment method in PreparedStatement class after multi-stage transfer through ParameterHandler interface.

Public class CallableStatementHandler extends BaseStatementHandler {/ * Parameter handling of statements * @ param statement SQL statement * @ throws SQLException * / @ Override public void parameterize (Statement statement) throws SQLException {/ / Registration of output parameters registerOutputParameters ((CallableStatement) statement); / / processing of input parameters parameterHandler.setParameters ((CallableStatement) statement) }} the above is the content of this article on "the source code analysis of mybatis's executor package sentence processing function". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report