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

  • throughsqlMapper.openSession()Get a “tool” for actionable dataSqlSession, includingsqlMapperIt 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 onstatementIdGet aMappedStatementYou should be able to see this in the initialization sectionMappedStatementIs initialized during initialization and saved in the object containingSQLAll information needed to execute:

  • Access to theMappedStatementAfter that, the executor is called to executeSQL;
  • Here we can execute the test methods directly under the source packageorg.apache.ibatis.session.SqlSessionTest#shouldSelectOneAuthor, as shown below:

  • What needs to be explained is: the actuatorExcutorThe 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#queryMethod is called afterBaseExcutor#queryMethod, and the subsequent call process is shown in the following figure

  • BaseEexcutorHandle level 1 cache, getBoundSqlOperations such as;
  • SimpleExecutorAccess to theConfigurationTo createStatementHandler,ParameterHandler,ResultHandler; createConnection, the callhandler.parameterize(stmt)Processing parameters;
  • PreparedStatementHandlerprecompiledSQL, the callps.execute();performSQL;
  • DefaultResultSetHandlerProcess 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#getMapperSource code analysis