MyBatis源码分析——SqlSessionFactory实例的产生过程

来源:互联网 发布:淘宝如何设置活动秒杀 编辑:程序博客网 时间:2024/05/16 05:32

简单介绍下:SqlSessionFactory

依靠工厂(SqlSessionFactory)来生成会话(SqlSession)。MyBatis 提供了两个 SqlSessionFactory 的实现类,DefaultSqlSessionFactory 和 SqlSessionManager,MyBatis 使用的是前者。

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();}

可见 SqlSessionFactory 接口中的所有方法都是为了获得会话(SqlSession)。
SqlSessionFactory 通过 SqlSessionFactoryBuilder 获得。

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);  }}

从源代码可以看到,可以由三种方式构建SqlSessionFactory:

  1. InputStream(字节流)
  2. Reader(字符流)
  3. Configuration

一般的,我们这样构造 SqlSessionFactory

private SqlSessionFactory sqlSessionFactory;InputStream in = Resources.getResourceAsStream("mybatis-config.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);in.close();

当然也可以使用编程构造方式构造 Configuration 来生成 SqlSessionFactory

DataSource dataSource = ...;        TransactionFactory transactionFactory = new JdbcTransactionFactory();        Environment environment = new Environment("development", transactionFactory, dataSource);        Configuration configuration = new Configuration(environment);        configuration.setLazyLoadingEnabled(true);        configuration.getTypeAliasRegistry().registerAlias(Person.class);        configuration.addMapper(Person.class);        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

基于XML文件的这种构造方式,通过从XML中读取信息后,也是构造出Configuration对象之后再继续进行SqlSessionFactory的构建工作的,只是多了些XML的解析工作。

XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);SqlSessionFactory sqlSessionFactory = DefaultSqlSessionFactory(parser.parse());

无论是字节流还是字符流,它们最终都是把读取xml配置文件的流输送给XML的解析对象XMLConfigBuilder,而XMLConfigBuilder对象的终极目的就是parse注入的流成为Configuration对象,而Configuration对象几乎将配置文件中的配置标签一一对应的解析成了与之一致的对象,他就是mybatis框架的配置中心。
所以说Configuration等价于MyBatis的配置文件。

参考:
http://www.cnblogs.com/hzhuxin/p/3349836.html

阅读全文
0 0
原创粉丝点击