• Mybatis 源码阅读 (一)


    先从一张图说起吧,这个是mybatis源码包的目录。刚开始看只能从名字判断个大概,并不是特别理解,一个一个包来吧。先贴一段代码:

    1 public void Test() throws FileNotFoundException{
    2         String configLocation = "myBatis-config.xml";
    3         Reader reader = new FileReader(new File(configLocation));
    4         SqlSessionFactory sessionfactory = new SqlSessionFactoryBuilder().build(reader);
    5         SqlSession sqlsession = sessionfactory.openSession();
    6     }

    这个在熟悉不过了,这是构建sqlsession的大致流程;reader就不说了,首先看一下SqlSessionFactoryBuilder

    /**
     *    Copyright 2009-2015 the original author or authors.
     *
     *    Licensed under the Apache License, Version 2.0 (the "License");
     *    you may not use this file except in compliance with the License.
     *    You may obtain a copy of the License at
     *
     *       http://www.apache.org/licenses/LICENSE-2.0
     *
     *    Unless required by applicable law or agreed to in writing, software
     *    distributed under the License is distributed on an "AS IS" BASIS,
     *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     *    See the License for the specific language governing permissions and
     *    limitations under the License.
     */
    package org.apache.ibatis.session;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.Reader;
    import java.util.Properties;
    
    import org.apache.ibatis.builder.xml.XMLConfigBuilder;
    import org.apache.ibatis.exceptions.ExceptionFactory;
    import org.apache.ibatis.executor.ErrorContext;
    import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
    
    /*
     * Builds {@link SqlSession} instances.
     *
     */
    /**
     * @author Clinton Begin
     */
    public class SqlSessionFactoryBuilder {
    
      public SqlSessionFactory build(Reader reader) {
        return build(reader, null, null);
      }
    
      public SqlSessionFactory build(Reader reader, String environment) {
        return build(reader, environment, null);
      }
    
      public SqlSessionFactory build(Reader reader, Properties properties) {
        return build(reader, null, properties);
      }
    
      public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
        try {
          XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
          return build(parser.parse());
        } catch (Exception e) {
          throw ExceptionFactory.wrapException("Error building SqlSession.", e);
        } finally {
          ErrorContext.instance().reset();
          try {
            reader.close();
          } catch (IOException e) {
            // Intentionally ignore. Prefer previous error.
          }
        }
      }
    
      public SqlSessionFactory build(InputStream inputStream) {
        return build(inputStream, null, null);
      }
    
      public SqlSessionFactory build(InputStream inputStream, String environment) {
        return build(inputStream, environment, null);
      }
    
      public SqlSessionFactory build(InputStream inputStream, Properties properties) {
        return build(inputStream, null, properties);
      }
    
      public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
        try {
          XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
          return build(parser.parse());
        } catch (Exception e) {
          throw ExceptionFactory.wrapException("Error building SqlSession.", e);
        } finally {
          ErrorContext.instance().reset();
          try {
            inputStream.close();
          } catch (IOException e) {
            // Intentionally ignore. Prefer previous error.
          }
        }
      }
        
      public SqlSessionFactory build(Configuration config) {
        return new DefaultSqlSessionFactory(config);
      }
    
    }
    

      

    这是sqlsessionfactorybuilder的源码,最重要的就是build方法,然后就是不同形式的参数,最终会有一个XMLConfigBuilder的实例对象,

    调用他的parse方法会返回一个Configuration实例对象,Configration是整个mybatis配置的一个类里面包含了各种配置,然后调用build(Configuration config)方法返回一个SqlSessionFactory接口的一个实现类 DefaultSqlSessionFactory。返回一个DefaultSqlSessionFactory实例后就可以创建SQlsession对象了。sqlsession的创建过程还没看懂,慢慢更新吧。

    这里面的XMLConfigBuilder 是一个Xml解析类,使用了jdk自带的XPath类库解析

    解析后会把所有的配置放到上面到的configration实例中了,供其他类组件读取配置信息。 这里没贴全部源码,有兴趣的可以看看,感觉写的挺奇妙的。接下来会一个包一个包的读了

  • 相关阅读:
    Git是如何存储对象的
    原来自己一直平凡着 2015-10-20
    把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列。
    #define XXX do{...}while(0)
    函数的递归调用例子学习
    MAC OSX 下安装 CTAGS
    MAC OSX 下安装Cscope
    python画图
    python读取文件内容方法
    python变量传递给系统命令的方法
  • 原文地址:https://www.cnblogs.com/TakeaHeader/p/6297283.html
Copyright © 2020-2023  润新知