zoukankan      html  css  js  c++  java
  • Mybatis底层源码执行流程

    1.通过类加载器,加载了config.xml文件

    2.通过SqlSessionFactoryBuilder.build(resource)这个方法进行了config.xml的解析,解析为Configuration对象-------代码如下
    XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
    return this.build(parser.parse());----parser.parse()这里返回的是一个Configuration对象----在parse方法中,实现了以下方法

    3.this.parseConfiguration(this.parser.evalNode("/configuration"));
    在parseConfiguration方法中,进行具体的标签属性解析为对象,包括了对mappers的解析,具体方法名为mapperElement,在这方法中,加载了Mapper文件,以及对mapper中的属性进行了解析,解析为MapperStatement对象,并且把一个一个的MapperStatement对象加进了Configuration中,configuration.addMappedStatement(statement);最后返回一个Configuration对象

    4.在最后,最后一个功能:构建了一个DefaultSqlSessionFactory
    public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
    }

    5.方法sqlSessionFactory.openSession();实际执行的是DefaultSqlSessionFactory
    方法,因为第四步已经给我们new出来了,在DefaultSqlSessionFactory中调用openSession方法,在openSession方法中又调用了
    this.openSessionFromDataSource这个方法,在这个方法中创建了executor方法等,还new了一个DefaultSqlSession方法====new DefaultSqlSession(this.configuration, executor, autoCommit);这就是sqlsession的实现类,最后得到了sqlSession对象(执行增删改查等功能)

    6.Sqlsession.getMapper("User.dao")

    7.在DefaultSqlSession中调用getMapper();

    8.在configuration中调用getMapper();

    9.在mapperRegistry中调用getMapper();并且调用mapperProxyFactory.newInstance(sqlSession);

    10.在MapperProxyFactory中首先调用newInstance(Sqlsession),在调用newInstance(mapperProxy)并创建代理Proxy.newProxyInstance(加载器,接口,MapperProxy)

    11.在MapperProxy中负责实现了InvocationHandler的具体功能

    12.return代理对象mapper给我们,接着使用mapper调用具体的方法

    13.因为在MapperProxy中调用MapperMethod方法,此方法首先通过SqlCommand获取namespace.id以及方法的类型,通过MethodSignature处理返回值和参数。并调用MapperMethod方法中execute的方法,在方法中根据相应的方法类型(switch case),直接调用sqlSession.update等方法,完成了与Sqlsession建立连接

    14.因为sqlsession是接口,所在在它的实现类DefaultSqlSession中调用update方法,并在update方法中执行executor.update

    15.跳转到SimpleExecutor执行doUpdate方法,并在其中new一个StatementHandler,使用Statementhandler在调用update方法-----handler.update(stmt);

    16.因为StatementHandler是接口类型,所以跳转到它的实现类SimpleStatementHandler中,在这儿执行SimpleStatementHandler.update();在updata方法中执行了statement.execute(sql);这就是最后执行sql语句的位置,这其中就是jdbc的底层statement。

  • 相关阅读:
    vue Ant Design 树形控件拖动限制
    defineProperty介绍及使用
    webpack 配置入门
    vscode 插件
    解决输入框自动填充账号密码的问题
    css 动画
    vue按钮权限控制
    git操作
    TCP和UDP的区别
    通信协议 HTTP TCP UDP
  • 原文地址:https://www.cnblogs.com/wpy188/p/14593007.html
Copyright © 2011-2022 走看看