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

What is the function of StatementHandler in Mybatis

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail what the role of StatementHandler in Mybatis is, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Mybatis-3.4.6.release.

Figure 1

StatementHandler is a unified encapsulation of CallableStatement, PreparedStatement and Statement. CallableStatement inherits PreparedStatement,PreparedStatement and inherits Statement in JDK.

What's the difference between these StatementHandler? let's take a look. RoutingStatementHandler is very special. It uses a delegation mode, such as List-1.

List-1

Public class RoutingStatementHandler implements StatementHandler {private final StatementHandler delegate; public RoutingStatementHandler (Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {switch (ms.getStatementType ()) {case STATEMENT: delegate = new SimpleStatementHandler (executor, ms, parameter, rowBounds, resultHandler, boundSql); 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 ());}}

For example, List-1, select SimpleStatementHandler, PreparedStatementHandler, and CallableStatementHandler according to the value of MappedStatement.getStatementType (). CallableStatementHandler handles stored procedures, but what's the difference between SimpleStatementHandler and PreparedStatementHandler? The difference is that SimpleStatementHandler processing sql does not contain parameters, that is, sql does not contain parameters, while PreparedStatementHandler processing sql contains? Sql that needs to be precompiled:

List-2 is PreparedStatementHandler, parameters are set by ParameterHandler, List-3 is SimpleStatementHandler, and the implementation is empty.

List-2

Overridepublic void parameterize (Statement statement) throws SQLException {parameterHandler.setParameters ((PreparedStatement) statement);}

List-3

@ Overridepublic void parameterize (Statement statement) throws SQLException {/ / NCMA}

If you look at StatementHandler alone, it seems that there is no connection between its methods. We need to look at it in combination with Executor. Take SimpleExecutor as an example, as shown by List-4, see the doQuery method:

List-4

Public class SimpleExecutor extends BaseExecutor {... @ Override public List doQuery (MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {Statement stmt = null; try {Configuration configuration = ms.getConfiguration (); StatementHandler handler = configuration.newStatementHandler (wrapper, ms, parameter, rowBounds, resultHandler, boundSql); stmt = prepareStatement (handler, ms.getStatementLog ()); return handler.query (stmt, resultHandler);} finally {closeStatement (stmt);}}. Private Statement prepareStatement (StatementHandler handler, Log statementLog) throws SQLException {Statement stmt; Connection connection = getConnection (statementLog); stmt = handler.prepare (connection, transaction.getTimeout ()); handler.parameterize (stmt); return stmt;}}

Suppose you use SimpleExecutor, PreparedStatementHandler,SimpleExecutor.doQuery- > SimpleExecutor.prepareStatement

Call the PreparedStatementHandler.prepare method to get the prepareStatement of jdbc

PreparedStatementHandler.parameterize method, see List-2, set sql parameters to prepareStatement

Then in the doQuery method, call PreparedStatementHandler.query (), such as the List-5,query method, and then encapsulate it into an object through ResultSetHandler.handleResultSets.

List-5

Overridepublic List query (Statement statement, ResultHandler resultHandler) throws SQLException {PreparedStatement ps = (PreparedStatement) statement; ps.execute (); return resultSetHandler. HandleResultSets (ps);}

Executor- > StatementHandler- > parameterHandler/ResultSetHandler

For interface programming, template pattern is used and abstraction is used to reduce the coupling between the user and the concrete implementation class.

About what the role of StatementHandler in Mybatis is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report