Needless to say, the benefits of source code learning are small in volume and simple in logic, so I will write a series of articles to learn.
SqlSession
The Mybatis usage portal is located in the SqlSession in the org.apache.ibatis.session package. It is found that it is an interface, and there must be a default implementation class DefaultSqlSession in the org.apache.ibatis.session.defaults package. We have never new this class, and according to Java conventions, use the factory method in SqlSessionFactory. I found that it is also an interface, so there must be a default implementation class DefaultSqlSessionFactory. This class still does not need to be created by itself, and uses the factory method in SqlSessionFactoryBuilder.
DefaultSqlSession
The main methods in DefaultSqlSession:
1) Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds), delegated 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), and delegate to the executor.query(ms, wrapCollection(parameter), rowBounds, handler).
3) int update(String statement, Object parameter), delegate to executor.update(ms, wrapCollection(parameter)).
It can be seen that there is an executor to complete it in the end.
Executor
Executor is located in the org.apache.ibatis.executor package. It is an interface. The implementation classes are BaseExecutor and CachingExecutor. Among them, BaseExecutor is abstract and has three subclasses SimpleExecutor, ReuseExecutor and BatchExecutor. See the name and know the meaning. The 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), delegated as 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), delegated to abstract <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql).
3) int update(MappedStatement ms, Object parameter), delegate to abstract int doUpdate(MappedStatement ms, Object parameter).
The base class processes the public part, leaving it to the subclass implementation.
Let’s take a look at the main methods in SimpleExecutor:
1) List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql), delegated 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), delegate to handler.update(stmt).
It can be seen that it is finally handled by the handler.
StatementHandler
StatementHandler is located in the org.apache.ibatis.executor.statement package. It is an interface. The implementation classes are BaseStatementHandler and RoutingStatementHandler. BaseStatementHandler is abstract and has three subclasses SimpleStatementHandler, PreparedStatementHandler and CallableStatementHandler. These three should be familiar with, and they should handle SQL statements without parameters, parameterized SQL statements and stored procedures respectively. Let’s look at the main methods in SimpleStatementHandler:
1) List<E> query(Statement statement, ResultHandler resultHandler), delegate to statement.execute(sql).
2) Cursor<E> queryCursor(Statement statement), delegate to statement.execute(sql).
3) int update(Statement statement), delegate to statement.execute(sql).
Finally, SQL is executed by statement. This returns to the java.sql package.
Mybatis mainly completes the encapsulation processing of SQL parameters, obtains the result set and generates objects, and leaves the construction process of SQL statements to the user. Mybatis' author designed it this way. Although the framework is semi-automatic overall, its flexibility has been greatly increased.