参考来自:《深入浅出MyBatis技术原理与实践-第6章 》
1.SqlSessionFactory
SqlSessionFactory是一个接口,最重要的功能是提供SqlSession。
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。
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来构建。
SqlSessionFactory中提供了很多build方法,最终调用的都是下面两个中的一个,然后调用第三个build:生成一个defaultSqlSessionFactory(configuration config)。
Configuration类中则保存了配置相关的参数,全局唯一。
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的构建过程:
- mybatis通过SqlSessionFactoryBuilder去构建SqlSessionFactory。
- SqlSessionFactoryBuilder中提供了SqlXMLConfigBuilder读取XML配置文件中的信息,保存至Configuration类的单例中。
- 然后SqlSessionFactoryBuilder实例化DefaultSqlSessionFactory。