參考來自:《深刻淺出MyBatis技術原理與實踐-第6章 》java
1.SqlSessionFactorysql
SqlSessionFactory是一個接口,最重要的功能是提供SqlSession。apache
1 public interface SqlSessionFactory { 2 3 SqlSession openSession(); 4 5 SqlSession openSession(boolean autoCommit); 6 SqlSession openSession(Connection connection); 7 SqlSession openSession(TransactionIsolationLevel level); 8 9 SqlSession openSession(ExecutorType execType); 10 SqlSession openSession(ExecutorType execType, boolean autoCommit); 11 SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level); 12 SqlSession openSession(ExecutorType execType, Connection connection); 13 14 Configuration getConfiguration(); 15 16 }
SqlSessionFactory有一個默認的實現類DefaultSqlSessionFactory。session
16 package org.apache.ibatis.session.defaults; 17 18 import java.sql.Connection; 19 import java.sql.SQLException; 20 21 import org.apache.ibatis.exceptions.ExceptionFactory; 22 import org.apache.ibatis.executor.ErrorContext; 23 import org.apache.ibatis.executor.Executor; 24 import org.apache.ibatis.mapping.Environment; 25 import org.apache.ibatis.session.Configuration; 26 import org.apache.ibatis.session.ExecutorType; 27 import org.apache.ibatis.session.SqlSession; 28 import org.apache.ibatis.session.SqlSessionFactory; 29 import org.apache.ibatis.session.TransactionIsolationLevel; 30 import org.apache.ibatis.transaction.Transaction; 31 import org.apache.ibatis.transaction.TransactionFactory; 32 import org.apache.ibatis.transaction.managed.ManagedTransactionFactory; 33 34 /** 35 * @author Clinton Begin 36 */ 37 public class DefaultSqlSessionFactory implements SqlSessionFactory { 38 39 private final Configuration configuration; 40 41 public DefaultSqlSessionFactory(Configuration configuration) { 42 this.configuration = configuration; 43 } 84 85 @Override 86 public Configuration getConfiguration() { 87 return configuration; 88 } 89 //其他略
144 145 }
在對SqlSessionFactory的構建中,直接使用構造方法去構建是有困難的,這樣會將大量的邏輯放在構造方法中。所以採用的是builder模式,使用SqlSessionFactoryBuilder來構建。mybatis
SqlSessionFactory中提供了不少build方法,最終調用的都是下面兩個中的一個,而後調用第三個build:生成一個defaultSqlSessionFactory(configuration config)。app
Configuration類中則保存了配置相關的參數,全局惟一。ide
1 package org.apache.ibatis.session; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.Reader; 6 import java.util.Properties; 7 8 import org.apache.ibatis.builder.xml.XMLConfigBuilder; 9 import org.apache.ibatis.exceptions.ExceptionFactory; 10 import org.apache.ibatis.executor.ErrorContext; 11 import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory; 12 13 public class SqlSessionFactoryBuilder { 14 15 public SqlSessionFactory build(Reader reader, String environment, Properties properties) { 16 try { 17 XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties); 18 return build(parser.parse()); 19 } catch (Exception e) { 20 throw ExceptionFactory.wrapException("Error building SqlSession.", e); 21 } finally { 22 ErrorContext.instance().reset(); 23 try { 24 reader.close(); 25 } catch (IOException e) { 26 // Intentionally ignore. Prefer previous error. 27 } 28 } 29 } 30 31 public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) { 32 try { 33 XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties); 34 return build(parser.parse()); 35 } catch (Exception e) { 36 throw ExceptionFactory.wrapException("Error building SqlSession.", e); 37 } finally { 38 ErrorContext.instance().reset(); 39 try { 40 inputStream.close(); 41 } catch (IOException e) { 42 // Intentionally ignore. Prefer previous error. 43 } 44 } 45 } 46 47 public SqlSessionFactory build(Configuration config) { 48 return new DefaultSqlSessionFactory(config); 49 } 50 //其他略 51 }
綜上所述,SqlSessionFactory的構建過程:ui