(一)mybatsi 流程、原理

1.mybatis开发流程

1.1 添加MyBatis依赖jar

 

1.2 开发一个实体映射类

 

1.3 开发一个SQL映射文件

在src/main/resource下创建与当前表对应的SQL映射文件用于声明SQL语句

 

1.4 开发MyBatis核心配置文件

   在src/main/resources下创建MyBatis-config.xml作为核心配置文件

1.5 开发MyBatis基本调用流程

2.mybatis工作原理

总结:

1.      mybatis的工作流程

  1. 将框架中的配置文件保存到configuration对象
  2. 将configuration作为属性交给我们当前DefaultSqlSessionFactory实例对象(DefaultSqlSessionFactory是SqlSessionFactory的实现类),到此我们就想数据库配置信息加载到sqlSessionFactory中,创建了一个sqlSessionFactory工厂。
  3. 创建好了sqlSessionFactory工厂之后就要生成sqlSession对象。

4.      在创建SqlSession对象提供属性

        1) Configuration对象

        2) dirty:true   sql语句执行完毕后   可以事务提交

                  false  sql语句执行发送错误  事务进行回滚

        3) Executor执行器对象:

                  创建Statement对象,在创建过程中依靠MapperStatement对象将赋值                                       内容与sql占位符进行绑定

         5.      SqlSession.commit(): 根据此时dirty属性决定提交和回滚

       6.      SqlSession.close(); 

2.      Mybatis如何将sql语句id与sql语句进行定位

在这个MapperAnnotationBuilder中会判断是否有定义命名空间,如果没有或者定义出错就会报出错误

private void loadXmlResource() {
    if (!this.configuration.isResourceLoaded("namespace:" + this.type.getName())) {
        String xmlResource = this.type.getName().replace('.', '/') + ".xml";
        InputStream inputStream = null;

        try {
            inputStream = Resources.getResourceAsStream(this.type.getClassLoader(), xmlResource);
        } catch (IOException var4) {
            ;
        }

        if (inputStream != null) {
            XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, this.assistant.getConfiguration(), xmlResource, this.configuration.getSqlFragments(), this.type.getName());
            xmlParser.parse();
        }
    }

}

3.      mybatis如何将数据绑定到sql命令

public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {// parameter是传递进来的对象参数
    Statement stmt = null;//JDBC的Statement对象
    int var6;
    try {
        Configuration configuration = ms.getConfiguration();
        StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, (ResultHandler)null, (BoundSql)null);
        stmt = this.prepareStatement(handler, ms.getStatementLog());
        //将对象通过JDBC的prepareStatement实现类封装成完整的SQL语句

var6 = handler.update(stmt);
    } finally {
        this.closeStatement(stmt);
    }

    return var6;
}

4.      mybatis如何输送sql

在这个XMLConfigBuilder中读取配置的标签的内容

public Configuration parse() {
    if (this.parsed) {
        throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    } else {
        this.parsed = true;
        this.parseConfiguration(this.parser.evalNode("/configuration"));
        return this.configuration;
    }
}

private void parseConfiguration(XNode root) {
    try {
        this.propertiesElement(root.evalNode("properties"));
        Properties settings = this.settingsAsProperties(root.evalNode("settings"));
        this.loadCustomVfs(settings);
        this.typeAliasesElement(root.evalNode("typeAliases"));
        this.pluginElement(root.evalNode("plugins"));
        this.objectFactoryElement(root.evalNode("objectFactory"));
        this.objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
        this.reflectorFactoryElement(root.evalNode("reflectorFactory"));
        this.settingsElement(settings);
        this.environmentsElement(root.evalNode("environments"));
        this.databaseIdProviderElement(root.evalNode("databaseIdProvider"));
        this.typeHandlerElement(root.evalNode("typeHandlers"));
        this.mapperElement(root.evalNode("mappers"));
    } catch (Exception var3) {
        throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + var3, var3);
    }
}

5.  SqlSession中的dirty属性赋值的目的

this.dirty = true;//赋值的目的:sqlSession的一个属性
MappedStatement ms = this.configuration.getMappedStatement(statement);//找到ID对应的sql语句

ErrorContext.instance().resource(ms.getResource()).activity("executing an update").object(ms.getId());//找到对应的mapper文件并且传递个ID匹配对应的sql语句

今天就先简单介绍下Mybatis的开发流程和与原理的个人总结,下一篇介绍的是对mybatis的配置文件标签的使用。

有问题邮箱:[email protected]

猜你喜欢

转载自www.cnblogs.com/fqh0324/p/11037601.html