(补充知识点: 1 byte(字节)=8 bit(位) 通常一个标准英文字母占一个字节位置,一个标准汉字占两个字节位置;字符的例子有:字母、数字系统或标点符号)
1.创建SqlSessionFactory
①Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); //获取mybatis配置文件的字符
注解:Resources类是在mybatis中定义的一个类;getResourceAsReader(String resource)方法返回的是一个Reader(字符)类型的值
getResourceAsReader(String resource)的源码
public static Reader getResourceAsReader(String resource) throws IOException {
Reader reader; //定义一个变量reader
if (charset == null) { //源码最前面定义了一个charset(字符集),此时为空
reader = new InputStreamReader(getResourceAsStream(resource)); //先以文件流的方式获取到资源,再将获取到的资源转化为字符
} else {
reader = new InputStreamReader(getResourceAsStream(resource), charset);
}
return reader; //返回获取到的文件字符
}
-----------------------------------------------------------------------------------------------------
以流的方式获取到资源的源码
public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
//classLoaderWrapper将ClassLoader进行了封装,因为ClassLoader是java用来加载配置文件的
InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
if (in == null) throw new IOException("Could not find resource " + resource);
return in; //将配置文件以文件流的形式返回
}
------------------------------------------------------------------------------------------------------
//SqlSession工厂建立者以返回的字符作为参数,建立SqlSession工厂,将配置文件中的各项参数赋值到sqlSessionFactory 中
②sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
建立SqlSession工厂源码
public SqlSessionFactory build(Reader reader) {
return build(reader, null, null);
}
-------------------------------------------------------------------------------------------------------
public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties); //建立xml配置文件解析器
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(Configuration config) {
return new DefaultSqlSessionFactory(config); //通过parser解析将mybatis配置文件的信息作为参数传递进来,生成 DefaultSqlSessionFactory
}
③session = sqlSessionFactory.openSession(); //新建一个sqlSession
------------------------------------------------------------------
public SqlSession openSession() {
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false); //参数值为(SIMPLE,null,false)
}
------------------------------------------------------------------
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
final Environment environment = configuration.getEnvironment(); //获取到数据库环境
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); //通过环境,获取到事务工厂为jdbcTransactionFactory
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); //新建事务
final Executor executor = configuration.newExecutor(tx, execType); //新建代理执行器,里面有plugin(插件)的各项信息
return new DefaultSqlSession(configuration, executor, autoCommit); //新建DefaultSqlSession
} 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();
}
}
----------------------------------------------------------------------
就是将各个参数赋值
public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
this.configuration = configuration;
this.executor = executor;
this.dirty = false;
this.autoCommit = autoCommit;
}
④到目前为止,sqlSession已经创建完毕,接下来可以使用了