1. 程式人生 > >MyBatis之啟動分析(一)

MyBatis之啟動分析(一)

ytao

前言

MyBatis 作為目前最常用的持久層框架之一,分析其原始碼,對我們的使用過程中可更好的運用它。本系列基於mybatis-3.4.6進行分析。 MyBatis 的初始化工作就是解析主配置檔案,對映配置檔案以及註解資訊。然後儲存在org.apache.ibatis.session.Configuration,供後期執行資料請求的相關呼叫。 Configuration 裡有大量配置資訊,在後面每涉及到一個相關配置,會進行詳細的分析。

<!-- more -->

啟動

    public static void main(String[] args) throws IOException {
        // 獲取配置檔案
        Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
        // 通過 SqlSessionFactoryBuilder 構建 sqlSession 工廠
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        // 獲取 sqlSession 例項
        SqlSession sqlSession = sqlSessionFactory.openSession();

        reader.close();
        sqlSession.close();
    }

分析

SqlSessionFactoryBuilder 類

SqlSessionFactoryBuilder 的build()是Mybatis啟動的初始化入口,使用builder模式載入配置檔案。 通過檢視該類,使用方法過載,有以下9個方法: SqlSessionFactoryBuilde類中的方法

方法過載最終實現處理的方法原始碼如下:

    public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
        try {
          // 例項化 XMLConfigBuilder,用於讀取配置檔案資訊
          XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
          // 解析配置資訊,儲存到 Configuration
          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.
          }
        }
      }
  • environment 是指定載入環境,預設值為 null。
  • properties 是屬性配置檔案,預設值為 null。 同時讀取配置檔案既可字元流讀取,也支援位元組流讀取。
    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.
          }
        }
      }

例項化 XMLConfigBuilder 類

通過 SqlSessionFactoryBuilder 中 XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties), 分析 XMLConfigBuilder例項化過程。 該類中有四個變數:

    private boolean parsed;
    private final XPathParser parser;
    private String environment;
    private final ReflectorFactory localReflectorFactory = new DefaultReflectorFactory();
  • parsed 是否解析,一次解析即可。用於標誌配置檔案只解析一次,true為已解析過。
  • parser 解析配置的解析器
  • environment 載入環境,即 SqlSessionFactoryBuilder 中的 environment
  • localReflectorFactory 用於建立和快取Reflector物件,一個類對應一個Reflector。因為引數處理、結果對映等操作時,會涉及大量的反射操作。DefaultReflectorFactory實現類比較簡單,這裡不再進行講解。

XMLConfigBuilder構建函式實現:

    public XMLConfigBuilder(Reader reader, String environment, Properties props) {
        this(new XPathParser(reader, true, props, new XMLMapperEntityResolver()), environment, props);
    }

例項化 XPathParser 物件

首先例項化 XPathParser 物件,裡面定義了5個變數:

    private final Document document;
    private boolean validation;
    private EntityResolver entityResolver;
    private Properties variables;
    private XPath xpath;
  • document 儲存document物件
  • validation xml解析時是否驗證文件
  • entityResolver 載入dtd檔案
  • variables 配置檔案定義<properties>的值
  • xpath Xpath物件,用於對XML檔案節點的操作

XPathParser 物件建構函式有: XPathParser構造方法 函式裡面都處理了兩件事:

    public XPathParser(Reader reader, boolean validation, Properties variables, EntityResolver entityResolver) {
        commonConstructor(validation, variables, entityResolver);
        this.document = createDocument(new InputSource(reader));
    }
  1. 初始化賦值,和建立XPath物件,用於對XML檔案節點的操作。
    private void commonConstructor(boolean validation, Properties variables, EntityResolver entityResolver) {
        this.validation = validation;
        this.entityResolver = entityResolver;
        this.variables = variables;
        // 建立Xpath物件,用於對XML檔案節點的操作
        XPathFactory factory = XPathFactory.newInstance();
        this.xpath = factory.newXPath();
    }
  1. 建立Document物件並賦值到document變數, 這裡屬於Document建立的操作,不再詳細講述,不懂可以點選這裡檢視API
    private Document createDocument(InputSource inputSource) {
        // important: this must only be called AFTER common constructor
        try {
          // 例項化 DocumentBuilderFactory 物件,用於建立 DocumentBuilder 物件
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          // 是否校驗文件
          factory.setValidating(validation);
          // 設定 DocumentBuilderFactory 的配置
          factory.setNamespaceAware(false);
          factory.setIgnoringComments(true);
          factory.setIgnoringElementContentWhitespace(false);
          factory.setCoalescing(false);
          factory.setExpandEntityReferences(true);
          // 建立 DocumentBuilder
          DocumentBuilder builder = factory.newDocumentBuilder();
          builder.setEntityResolver(entityResolver);
          builder.setErrorHandler(new ErrorHandler() {
            @Override
            public void error(SAXParseException exception) throws SAXException {
              throw exception;
            }
    
            @Override
            public void fatalError(SAXParseException exception) throws SAXException {
              throw exception;
            }
    
            @Override
            public void warning(SAXParseException exception) throws SAXException {
            }
          });
          // 載入檔案
          return builder.parse(inputSource);
        } catch (Exception e) {
          throw new BuilderException("Error creating document instance.  Cause: " + e, e);
        }
    }

XMLConfigBuilder建構函式賦值

    private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {
        super(new Configuration());
        ErrorContext.instance().resource("SQL Mapper Configuration");
        this.configuration.setVariables(props);
        this.parsed = false;
        this.environment = environment;
        this.parser = parser;
    }
  1. 初始化父類BaseBuilder的值。
  2. 將外部值賦值給物件。
  3. 將例項化的XPathParser賦值給parser

最後返回XMLConfigBuilder物件。

解析 XMLConfigBuilder 物件

通過 XMLConfigBuilder.parse() 解析配置資訊,儲存至Configuration。解析詳解在後面文章中進行分析。

    public Configuration parse() {
        // 是否解析過配置檔案
        if (parsed) {
          throw new BuilderException("Each XMLConfigBuilder can only be used once.");
        }
        // 標誌解析過,定義為 true
        parsed = true;
        // 解析 configuration 節點中的資訊
        parseConfiguration(parser.evalNode("/configuration"));
        return configuration;
    }

建立 SqlSessionFactory

DefaultSqlSessionFactory實現了SqlSessionFactory介面。 通過上面解析得到的Configuration,呼叫SqlSessionFactoryBuilder.build(Configuration config)建立一個 DefaultSqlSessionFactory

    public SqlSessionFactory build(Configuration config) {
        return new DefaultSqlSessionFactory(config);
    }

例項化DefaultSqlSessionFactory的過程,就是將Configuration傳遞給DefaultSqlSessionFactory成員變數configuration

    public DefaultSqlSessionFactory(Configuration configuration) {
        this.configuration = configuration;
    }

建立 SqlSession

通過呼叫SqlSessionFactory.openSession()建立SqlSession

    public interface SqlSessionFactory {
      // 預設建立
      SqlSession openSession();
    
      SqlSession openSession(boolean autoCommit);
      SqlSession openSession(Connection connection);
      SqlSession openSession(TransactionIsolationLevel level);
    
      SqlSession openSession(ExecutorType execType);
      SqlSession openSession(ExecutorType execType, boolean autoCommit);
      SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level);
      SqlSession openSession(ExecutorType execType, Connection connection);
    
      Configuration getConfiguration();
    
    }
  • autoCommit 是否自動提交事務,
  • level 事務隔離級別(共5個級別), 可檢視相關原始碼
  • connection 連線
  • execType 執行器的型別:SIMPLE(不做特殊處理), REUSE(複用預處理語句), BATCH(會批量執行)

因為上面DefaultSqlSessionFactory實現了SqlSessionFactory介面,所以進入到DefaultSqlSessionFactory檢視openSession()

    public SqlSession openSession() {
        return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
    }

openSession()方法最終實現程式碼如下:

    private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
        Transaction tx = null;
        try {
          // 獲取configuration中的載入環境
          final Environment environment = configuration.getEnvironment();
          // 獲取事務工廠
          final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
          // 建立一個事務
          tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
          // 生成一個處理器,事務儲存在處理器 BaseExecutor 中
          final Executor executor = configuration.newExecutor(tx, execType);
          // 例項化一個 DefaultSqlSession,DefaultSqlSession實現了SqlSession介面
          return new DefaultSqlSession(configuration, executor, autoCommit);
        } catch (Exception e) {
          // 異常情況下關閉事務
          closeTransaction(tx); // may have fetched a connection so lets call close()
          throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
        } finally {
          // 充值錯誤例項上下文
          ErrorContext.instance().reset();
        }
    }

生成處理器Configuration.newExecutor(Transaction transaction, ExecutorType executorType)

    public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
        // 預設為 ExecutorType.SIMPLE
        executorType = executorType == null ? defaultExecutorType : executorType;
        executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
        Executor executor;
        if (ExecutorType.BATCH == executorType) {
          executor = new BatchExecutor(this, transaction);
        } else if (ExecutorType.REUSE == executorType) {
          executor = new ReuseExecutor(this, transaction);
        } else {
          executor = new SimpleExecutor(this, transaction);
        }
        if (cacheEnabled) {
          executor = new CachingExecutor(executor);
        }
        executor = (Executor) interceptorChain.pluginAll(executor);
        return executor;
    }

ExecutorType.SIMPLE為例, BatchExecutor, ReuseExecutor同理: SimpleExecutor父類圖 至此,mybatis的啟動流程大致簡單的介紹到這裡,對mybatis的啟動初始化有個大致瞭解。接下將會針對單獨模組進行詳細分析。

我的公眾號 ytao 我的公眾號