Mybatis source code analysis (I) opening

Needless to say, mybatis has a small amount of source code and simple logic. I will write a series of articles to learn.

sqlSession

The use portal for mybatis is located at org apache. ibatis. Sqlsession in the session package is found to be an interface, and there must be a default implementation class org apache. ibatis. session. Defaultsqlsession in the defaults package. We have never used this class new. We use the factory method in sqlsessionfactory according to Java conventions. It is found that it is also an interface, and there must be a default implementation class defaultsqlsessionfactory. This class still does not need to be created by itself, but uses the factory method in sqlsessionfactorybuilder.

DefaultsqlSession

Main methods in defaultsqlsession:

1) Cursor < T > select cursor (string statement, object parameter, rowbounds, rowbounds) and delegate it to the executor queryCursor(ms,wrapCollection(parameter),rowBounds)。

2) List < E > selectlist (string statement, rowbounds, rowbounds) and void select (string statement, rowbounds, rowbounds, resulthandler handler) are delegated to the executor query(ms,rowBounds,handler)。

3) Int update (string statement, object parameter), delegated to the executor update(ms,wrapCollection(parameter))。 It can be seen that in the end, there is an executor to complete.

Executor

Executor is located at org apache. ibatis. In the executor package, is an interface, and the implementation classes are baseexecutor and cacheingexector. Baseexecutor is abstract and has three subclasses: simpleexecution, reuseexecution and batchexecutor. See the meaning of the name. Main methods in baseexecutor:

1) List < E > query (mappedstatement MS, resulthandler, resulthandler, cachekey, boundsql, boundsql) and list < E > queryfromdatabase (mappedstatement MS, boundsql, boundsql) are delegated as abstract < E > List < E > doquery (mappedstatement MS, boundsql, boundsql).

2) Cursor < E > querycursor (mappedstatement MS, rowboundaries, rowboundaries) is delegated to abstract abstract < E > cursor < E > doquerycursor (mappedstatement MS, boundsql, boundsql).

3) Int update (mappedstatement MS, object parameter) is delegated to the abstract abstract int doupdate (mappedstatement MS, object parameter).

The base class handles the common part, which is left to the subclass implementation.

Let's look at the main methods in simpleexecution:

1) List < E > doquery (mappedstatement MS, boundsql, boundsql), delegate to handler< E>query(stmt,resultHandler)。

2) Cursor < E > doquerycursor (mappedstatement MS, boundsql, boundsql), which is delegated to the handler< E>queryCursor(stmt)。

3) Int doupdate (mappedstatement MS, object parameter), delegate to handler update(stmt)。

It can be seen that it is finally handled by the handler.

StatementHandler

Statementhandler is located at org apache. ibatis. executor. In the statement package, is an interface, and the implementation classes are basestatementhandler and routingstatementhandler. Basestatementhandler is abstract and has three subclasses: simplestationhandler, preparedstatementhandler and callablestatementhandler. These three should be familiar, dealing with SQL statements without parameters, parameterized SQL statements and stored procedures respectively. Let's look at the main methods in simplestationhandler:

1) List < E > query (statement, resulthandler, resulthandler), delegate to statement execute(sql)。

2) Cursor < E > querycursor (statement statement), delegated to statement execute(sql)。

3) Int update (statement statement), delegated to statement execute(sql)。

Finally, the statement executes the SQL. This brings us back to Java SQL package.

Mybatis mainly completes the encapsulation of SQL parameters, the acquisition of result sets and the generation of objects, while leaving the construction process of SQL statements to users. The author of mybatis deliberately designed this way. Although the framework is semi-automatic as a whole, its flexibility has been greatly increased.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>