A brief analysis of MyBatis source code (a) at the beginning

  • 2020-05-17 05:27:23
  • OfStack

The benefits of source learning needless to say, Mybatis source less, simple logic, will write a series of articles to learn.

SqlSession

Mybatis entry point is located at the use of org. apache. ibatis. The SqlSession session package, found that it is an interface, there must be a default implementation class org. apache. ibatis. session. defaults DefaultSqlSession in the package. We have never used new in this class, and use the factory methods in SqlSessionFactory as per Java convention. Discover that it is also an interface, and there must be a default implementation class DefaultSqlSessionFactory. This class still doesn't have to be created by itself, using the factory methods in SqlSessionFactoryBuilder.

DefaultSqlSession

Main methods in DefaultSqlSession:

1) Cursor < T > selectCursor(String statement, Object parameter, RowBounds rowBounds), entrusted to executor.queryCursor (ms, wrapCollection(parameter), rowBounds).

2) List < E > selectList(String statement, Object parameter, RowBounds rowBounds) and void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) are entrusted to executor.query (ms, wrapCollection(parameter), rowBounds, handler).

3) int update(String statement, Object parameter), entrusted to executor.update (ms, wrapCollection(parameter)).
It can be seen that executor has been used to complete all of them.

Executor

Executor in org. apache. ibatis. executor package, is an interface, the implementation class is BaseExecutor and CachingExecutor. BaseExecutor is abstract, and there are three subclasses, SimpleExecutor, ReuseExecutor and BatchExecutor. Main methods in BaseExecutor:

1) List < E > query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) and List < E > queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql), delegate is abstract abstract < E > List < E > doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql)

2) Cursor < E > queryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds), delegates to the abstract abstract < E > Cursor < E > doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql)

3) int update(MappedStatement ms, Object parameter), delegate to the abstract abstract int doUpdate(MappedStatement ms, Object parameter).

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

Let's look at the main methods in SimpleExecutor:

1) List < E > doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql), entrusted to handler. < E > query (stmt resultHandler).

2) Cursor < E > doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql), entrusted to handler. < E > queryCursor (stmt).

3) int doUpdate(MappedStatement ms, Object parameter), entrusted to handler update(stmt).

As you can see, handler ultimately handles this.

StatementHandler

StatementHandler in org. apache. ibatis. executor. statement package, is an interface, the implementation class is BaseStatementHandler and RoutingStatementHandler. BaseStatementHandler is abstract and has three subclasses: SimpleStatementHandler, PreparedStatementHandler, and CallableStatementHandler. These three should be familiar, dealing with no-argument sql statements, parameterized sql statements, and stored procedures, respectively. Let's look at the main methods in SimpleStatementHandler:

1) List < E > query(Statement statement, ResultHandler resultHandler), entrusted to statement.execute (sql).

2) Cursor < E > queryCursor(Statement statement), entrusted to statement.execute (sql).

3) int update(Statement statement), entrusted to statement.execute(sql).

sql is finally executed by statement. This brings us back to the java.sql package.

Mybatis mainly completes the encapsulation processing of sql parameters, the acquisition of result sets and the generation of objects, while leaving the construction process of sql statements to the user. The authors of Mybatis did this on purpose. Although the framework as a whole is semi-automatic, flexibility has been greatly increased.


Related articles: