• 周六. 10 月 12th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Mybatis source code | a single query result (` select * from t)_ Where id =?) the execution process

King Wang

1 月 3, 2022

Environmental Science :mybatis edition 3.4.5
mysql Database driven version :5.1.6

MyBatis Source code :
A single query result (select * from t_user where id = ?)SQL Implementation method chain tracking process


sqlSession.getMapper Got mapper It’s a dynamic proxy object , yes MapperProxy type :

UserDao mapper = sqlSession.getMapper(UserDao.class);
User user = mapper.findById(2);

So the execution entry can be from org.apache.ibatis.binding.MapperProxy#invoke Start tracking , Here are the layers of depth , View the method call chain of the execution process , The design patterns involved are : A dynamic proxy 、 Decorator mode 、 The chain of responsibility model (XXXHandler In class )、 Command mode ;

At the bottom is the call JDBC The execution code of , We can trace connection That’s what you know mysql In the drive bag com.mysql.jdbc.JDBC4Connection type , This is also the database driver using the decorator pattern Connection Instance object of interface .

>org.apache.ibatis.binding.MapperProxy#invoke
>org.apache.ibatis.binding.MapperMethod#execute
>org.apache.ibatis.session.defaults.DefaultSqlSession#selectOne
>org.apache.ibatis.session.defaults.DefaultSqlSession#selectList
>org.apache.ibatis.executor.CachingExecutor#query
>org.apache.ibatis.executor.BaseExecutor#query
>org.apache.ibatis.executor.BaseExecutor#queryFromDatabase
>org.apache.ibatis.executor.SimpleExecutor#doQuery
>org.apache.ibatis.session.Configuration#newStatementHandler
>new org.apache.ibatis.executor.statement.RoutingStatementHandler Packed a PreparedStatementHandler
>org.apache.ibatis.executor.statement.BaseStatementHandler#BaseStatementHandler
>org.apache.ibatis.session.Configuration#newParameterHandler
>org.apache.ibatis.session.Configuration#newResultSetHandler
>org.apache.ibatis.executor.SimpleExecutor#prepareStatement
>org.apache.ibatis.executor.SimpleExecutor#prepareStatement The bottom is JDBC
>org.apache.ibatis.executor.statement.BaseStatementHandler#prepare
>org.apache.ibatis.executor.statement.PreparedStatementHandler#query
>java.sql.PreparedStatement#execute The method is JDBC Of , Can execute any SQL sentence
>org.apache.ibatis.executor.resultset.DefaultResultSetHandler#handleResultSets
>com.mysql.jdbc.StatementImpl#getResultSet
>org.apache.ibatis.logging.jdbc.PreparedStatementLogger#invoke
>org.apache.ibatis.executor.resultset.ResultSetWrapper#ResultSetWrapper
Logic after execution :
>org.apache.ibatis.binding.MapperProxy#invoke
>org.apache.ibatis.binding.MapperProxy#cachedMapperMethod

Compared with spring for ,mybatis Source code looks very comfortable , We’ll add a graphic description later , To make it clearer 、 More detailed module angle sharing .

发表回复