Mybatis核心流程以及源码解析

传统JDBC

  • 方式1-通过DriverManager
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, user, password);
PreparedStatement preparedStatement = connection.prepareStatement("insert into t_user (id, name) values (?, ?)");
preparedStatement.setInt(1, 12);
preparedStatement.setString(2, "this is paramter");
preparedStatement.executeUpdate();
// 如果执行的是查询,还要关闭ResultSet 
// resultSet.close();
preparedStatement.close();
connection.close();

  • 方式2-通过DataSource
DruidDataSource datasource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("select * from t_user");
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
   int id = resultSet.getInt(1);
   int gener = resultSet.getInt(2)
}
resultSet.close();
preparedStatement.close();
connection.close();

传统jdbc核心步骤说明
  • 加载驱动
  • 获取连接-Connection
  • 获取StateMent-PreparedStatement
  • 为SQL赋值-PreparedStatement.setXXX(paramterIndex, value)
  • 执行SQL-PreparedStatement.executeXXX();
  • 获得结果集-ResultSet
  • 依次关闭各种连接-XXX.close();

Mybatis核心流程

image.png
image.png

Mybatis核心组件

Configuration MyBatis所有的配置信息都保存在Configuration对象之中,配置文件中的大部分配置都会存储到该类中
SqlSession 作为MyBatis工作的主要顶层API,表示和数据库交互时的会话,完成必要数据库增删改查功能
Executor MyBatis执行器,是MyBatis 调度的核心,负责SQL语句的生成和查询缓存的维护
StatementHandler 封装了JDBC Statement操作,负责对JDBC statement 的操作,如设置参数等
ParameterHandler 负责对用户传递的参数转换成JDBC Statement 所对应的数据类型
ResultSetHandler 负责将JDBC返回的ResultSet结果集对象转换成List类型的集合
TypeHandler 负责java数据类型和jdbc数据类型(也可以说是数据表列类型)之间的映射和转换
MappedStatement MappedStatement维护一条<select|update|delete|insert>节点的封装
SqlSource 负责根据用户传递的parameterObject,动态地生成SQL语句,将信息封装到BoundSql对象中,并返回
BoundSql 表示动态生成的SQL语句以及相应的参数信息
下面是源码断点调试图
mabatis.gif
调用案例方法链(部分)
org.apache.ibatis.binding.MapperProxy#invoke
    org.apache.ibatis.binding.MapperMethod#execute
        org.mybatis.spring.SqlSessionTemplate#selectList(java.lang.String, java.lang.Object)
            org.mybatis.spring.SqlSessionTemplate.SqlSessionInterceptor#invoke
                org.apache.ibatis.session.defaults.DefaultSqlSession#selectList(java.lang.String, java.lang.Object, org.apache.ibatis.session.RowBounds)
                    org.apache.ibatis.plugin.Plugin#invoke
                        com.github.pagehelper.PageInterceptor#intercept
                            // BaseExecutor是SimpleExecutor的基类
                            org.apache.ibatis.executor.BaseExecutor#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)
                                org.apache.ibatis.executor.BaseExecutor#queryFromDatabase
                                    org.apache.ibatis.executor.SimpleExecutor#doQuery
                                        // 获取连接对象和语句对象
                                        org.apache.ibatis.executor.SimpleExecutor#prepareStatement
                                            // 获取连接对象
                                            org.apache.ibatis.executor.BaseExecutor#getConnection
                                                org.mybatis.spring.transaction.SpringManagedTransaction#getConnection
                                                    // 根据dataSouce获取对应的Connection实例.如SpringShardingDataSouce获取ShardingConnection
                                                    org.mybatis.spring.transaction.SpringManagedTransaction#openConnection
                                                        // 从DataSource中获取ShardingConnection
                                                        org.springframework.jdbc.datasource.DataSourceUtils#fetchConnection
                                            // 获取语句对象
                                            org.apache.ibatis.executor.statement.RoutingStatementHandler#prepare
                                                org.apache.ibatis.executor.statement.BaseStatementHandler#prepare
                                                org.apache.ibatis.executor.statement.BaseStatementHandler#instantiateStatement-接口
                                                    // 获取对应的  RoutingStatementHandler是PreparedStatementHandler基类
                                                    org.apache.ibatis.executor.statement.PreparedStatementHandler#instantiateStatement
                                                        org.apache.ibatis.logging.jdbc.ConnectionLogger#invoke
                                                        com.alibaba.druid.pool.DruidPooledConnection#prepareStatement(java.lang.String)
                                        // 设置参数
                                        org.apache.ibatis.executor.statement.RoutingStatementHandler#parameterize
                                        // 执行语句对象
                                        org.apache.ibatis.executor.statement.RoutingStatementHandler#query
                                            ps.execute();
                                            org.apache.ibatis.logging.jdbc.PreparedStatementLogger#invoke
                                                com.alibaba.druid.pool.DruidPooledPreparedStatement#execute
                                            // 对结果集处理
                                            org.apache.ibatis.executor.resultset.DefaultResultSetHandler#handleResultSets
MapperProxy案例说明
userMapper.queryById
    UserMapper接口的quryById
    MapperProxy实现UserMapper
    调用UserMapper.queryById可以理解为调用了MapperProxy的queryById方法,该方法又调用了invoke方法(动态代理,无法断点到MapperProxy的queryById方法)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,427评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,551评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,747评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,939评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,955评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,737评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,448评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,352评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,834评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,992评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,133评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,815评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,477评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,022评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,147评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,398评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,077评论 2 355