MyBatis源码分析:SqlSessionFactoryBuilder

MyBatis源码分析:SqlSessionFactoryBuilder

  • 1.SqlSessionFactoryBuilder

1.SqlSessionFactoryBuilder

功能:构造SqlSessionFactory的构造器,支持两种构造方式:1.从配置文件构造 2.从代码构造;

源码:

/**
 *    Copyright 2009-2019 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);
  }

}

其中,在MyBatis学习笔记1:Mybatis简介中使用的便是build方法之如下形式重载。

//以一个java.io.Reader对象作为输入,读取配置文件,并生成SqlSessionFactory对象
SqlSessionFactory build(Reader reader);

除此之外,build方法还包括了如下几种形式的重载:

//以java.io.Reader对象、java.util.Properties对象或String对象作为输入的重载方法
SqlSessionFactory build(Reader reader, String environment);
SqlSessionFactory build(Reader reader, Properties properties);
SqlSessionFactory build(Reader reader, String environment, Properties properties)
//以java.io.InputStream对象、java.util.Properties对象或String对象作为输入的重载方法
SqlSessionFactory build(InputStream inputStream);
SqlSessionFactory build(InputStream inputStream, String environment);
SqlSessionFactory build(InputStream inputStream, Properties properties);
SqlSessionFactory build(InputStream inputStream, String environment, Properties properties);
//以org.apache.ibatis.session.Configuration对象作为参数的重载方法
SqlSessionFactory build(Configuration config)

从源码中可以看出:

  1. 所有的包含Reader的build方法最终都会调用
    SqlSessionFactory build(Reader reader, String environment, Properties properties)方法;
  2. 所有包含InputStream的build方法最终都会调用
    SqlSessionFactory build(InputStream inputStream, String environment, Properties properties)方法;
  3. 这两个方法实现的唯一不同之处在于解析XML文件的时候,分别以Reader和InputStream作为参数。
  4. 最终,所有的build方法都会调用到SqlSessionFactory build(Configuration config)之中,此方法使用Configuration作为参数,构造了一个DefaultSqlSessionFactory对象。此方法为从代码中构造SqlSessionFactory提供了支持

你可能感兴趣的:(Mybatis)