1.源码分析入口测试类
通过源码给的测试类进行分析。 shouldSelectAllAuthors() 方法提供了从解析配置文件构建SqlSessionFactory到开启SqlSession,最后执行sqlSession的查询方法获取数据结果整个sql的执行生命流程,这也是我们需要的,本文通过这个测试类作为分析源码的入口。
@BeforeAll
static void setup() throws Exception {
createBlogDataSource();
final String resource = "org/apache/ibatis/builder/MapperConfig.xml";
final Reader reader = Resources.getResourceAsReader(resource);
sqlMapper = new SqlSessionFactoryBuilder().build(reader);
}
@Test
void shouldSelectAllAuthors() {
try (SqlSession session = sqlMapper.openSession(TransactionIsolationLevel.SERIALIZABLE)) {
List<Author> authors = session.selectList("org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAllAuthors");
assertEquals(2, authors.size());
}
}
2.SqlSessionFactory对象的创建
debug到SqlSessionManager.newInstance(reader)这一行。
继续往下走
进入build方法
可以看到上图的build()方法构建了SqlSessionFactory对象
3.xml配置文件节点解析
XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties)是通过构造函数构建XMLConfigBuilder对象。
从上图50行 build(parser.parse()),parse()方法一直往前走,来到XmlConfigBuilder#parseConfiguration(XNode root)方法。
这个方法里面的标签解析,其实就是对应了MapperConfig.xml里面的配置信息
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2009-2022 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
https://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.
-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="org/apache/ibatis/databases/blog/blog-derby.properties"/>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="false"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="false"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25"/>
</settings>
<typeAliases>
<typeAlias alias="Author" type="org.apache.ibatis.domain.blog.Author"/>
<typeAlias alias="Blog" type="org.apache.ibatis.domain.blog.Blog"/>
<typeAlias alias="Comment" type="org.apache.ibatis.domain.blog.Comment"/>
<typeAlias alias="Post" type="org.apache.ibatis.domain.blog.Post"/>
<typeAlias alias="Section" type="org.apache.ibatis.domain.blog.Section"/>
<typeAlias alias="Tag" type="org.apache.ibatis.domain.blog.Tag"/>
</typeAliases>
<typeHandlers>
<typeHandler javaType="String" jdbcType="VARCHAR" handler="org.apache.ibatis.builder.CustomStringTypeHandler"/>
</typeHandlers>
<objectFactory type="org.apache.ibatis.builder.ExampleObjectFactory">
<property name="objectFactoryProperty" value="100"/>
</objectFactory>
<plugins>
<plugin interceptor="org.apache.ibatis.builder.ExamplePlugin">
<property name="pluginProperty" value="100"/>
</plugin>
</plugins>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value=""/>
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/apache/ibatis/builder/AuthorMapper.xml"/>
<mapper resource="org/apache/ibatis/builder/BlogMapper.xml"/>
<mapper resource="org/apache/ibatis/builder/CachedAuthorMapper.xml"/>
<mapper resource="org/apache/ibatis/builder/PostMapper.xml"/>
<mapper resource="org/apache/ibatis/builder/NestedBlogMapper.xml"/>
</mappers>
</configuration>