origin
We have analyzed the session factory class instance SqlSessionFactory, and the source of the initialization configuration file load; If you haven’t seen children’s shoes, you can click the portal below to view them:
SqlSessionFactory Indicates the session factory
Initialize file load analysis
In this article, we will analyze the SQL execution process.
Create session & operation database
- through
sqlMapper.openSession()
Get a “tool” for actionable dataSqlSession
, includingsqlMapper
It refers to a session factorySqlSessionFactory
;
1.1,session.selectOne(statement,param)
Next, we call the session.selectOne method, from which we begin our source code journey;
Methods the full path: org. Apache. Ibatis. Session. Defaults. DefaultSqlSession# selectOne (Java. Lang. String, Java. Lang. Object)
- From the figure above, we can see that the first is based on
statementId
Get aMappedStatement
You should be able to see this in the initialization sectionMappedStatement
Is initialized during initialization and saved in the object containingSQL
All information needed to execute:
- Access to the
MappedStatement
After that, the executor is called to executeSQL
; - Here we can execute the test methods directly under the source package
org.apache.ibatis.session.SqlSessionTest#shouldSelectOneAuthor
, as shown below:
- What needs to be explained is: the actuator
Excutor
The chain of responsibility pattern is used (more on the design pattern in a later blog post).
1.2,CachingExecutor#query
-
From the above figure, we can intuitively see that when executing SQL by calling the executor, the first call is the query method in the cache executor.
-
Method full path: org.apache.ibatis.executor.CachingExecutor#query(org.apache.ibatis.mapping.MappedStatement, java.lang.Object, org.apache.ibatis.session.RowBounds, org.apache.ibatis.session.ResultHandler, org.apache.ibatis.cache.CacheKey, org.apache.ibatis.mapping.BoundSql)
CachingExecutor#query
Method is called afterBaseExcutor#query
Method, and the subsequent call process is shown in the following figure
BaseEexcutor
Handle level 1 cache, getBoundSql
Operations such as;SimpleExecutor
Access to theConfiguration
To createStatementHandler
,ParameterHandler
,ResultHandler
; createConnection
, the callhandler.parameterize(stmt)
Processing parameters;PreparedStatementHandler
precompiledSQL
, the callps.execute();
performSQL
;DefaultResultSetHandler
Process the result set and eventually return the data.
conclusion
-
Now we have completed the creation of SqlSessionFactory,SqlSession, and session.selectOne(Statement,param).
-
Next we will focus on sharing the way in which we often use database operation org. Apache. Ibatis. Session. SqlSession# getMapper;
-
Subordinate method dredge with return, we are not a share;
-
org.apache.ibatis.session.SqlSession#selectOne(java.lang.String) org.apache.ibatis.session.SqlSession#selectOne(java.lang.String, java.lang.Object) org.apache.ibatis.session.SqlSession#selectList(java.lang.String) org.apache.ibatis.session.SqlSession#selectList(java.lang.String, java.lang.Object) org.apache.ibatis.session.SqlSession#selectList(java.lang.String, java.lang.Object, Org. Apache. Ibatis. Session. RowBounds) / / the above five methods will be called to directly or indirectly, eventually After the source of execution flow with above org. Apache. Ibatis. Session. Defaults. DefaultSqlSession# selectList (Java. Lang. String, Java. Lang. Object, org.apache.ibatis.session.RowBounds)Copy the code
-
Please correct any omissions.
Recommendation by public account
Wechat public number: from Demo to toss source wechat signal: albert_ztym
Next up
org.apache.ibatis.session.SqlSession#getMapper
Source code analysis