概述
在 MyBatis 初始化过程中,会加载 mybatis-config.xml 配置文件、映射配置文件以及 Mapper 接口中的注解信息(包括属性配置、别名配置、拦截器配置、环境(数据源和事 务管理器)、Mapper配置等),解析后的配置信息会形成相应的对象并保存到 Configuration 对象中
mybatis简单的使用例子如下:
String resource = "mybatis-config.xml";Reader reader;try {//将XML配置文件构建为Configuration配置类reader = Resources.getResourceAsReader(resource);// 通过加载配置文件流构建一个SqlSessionFactory DefaultSqlSessionFactorySqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);// 数据源 执行器 DefaultSqlSessionSqlSession session = sqlMapper.openSession();try {// 执行查询 底层执行jdbc//User user = (User)session.selectOne("com.tuling.mapper.selectById", 1);UserMapper mapper = session.getMapper(UserMapper.class);System.out.println(mapper.getClass());User user = mapper.selectById(1L);System.out.println(user.getUserName());} catch (Exception e) {e.printStackTrace();}finally {session.close();}} catch (IOException e) {e.printStackTrace();}}
}
源码解析
可以看到mybatis初始化流程入口是 SqlSessionFactoryBuilder 类
org.apache.ibatis.session.SqlSessionFactoryBuilder#build(java.io.InputStream, java.lang.String, java.util.Properties)
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {try {//创建 XMLConfigBuilder 对象XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);// 执行 XML 解析// 创建 DefaultSqlSessionFactory 对象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.}}}
创建 XMLConfigBuilder 对象。
调用 XMLConfigBuilder#parse() 方法,执行 XML 解析,返回 Configuration 对象。
创建 DefaultSqlSessionFactory 对象。
构造方法
private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {// 创建 Configuration 对象super(new Configuration());ErrorContext.instance().resource("SQL Mapper Configuration");// 设置 Configuration 的 variables 属性this.configuration.setVariables(props);this.parsed = false;this.environment = environment;this.parser = parser;
}
XMLConfigBuilder#parse
org.apache.ibatis.builder.xml.XMLConfigBuilder#parse方法
public Configuration parse() {// 若已解析,抛出 BuilderException 异常if (parsed) {throw new BuilderException("Each XMLConfigBuilder can only be used once.");}// 标记已解析parsed = true;//解析 XML configuration 节点parseConfiguration(parser.evalNode("/configuration"));return configuration;
}
parseConfiguration
org.apache.ibatis.builder.xml.XMLConfigBuilder#parseConfiguration
private void parseConfiguration(XNode root) {try {//issue #117 read properties first// <1> 解析 <properties /> 标签propertiesElement(root.evalNode("properties"));// <2> 解析 <settings /> 标签Properties settings = settingsAsProperties(root.evalNode("settings"));// <3> 加载自定义 VFS 实现类loadCustomVfs(settings);// <4> 解析 <typeAliases /> 标签typeAliasesElement(root.evalNode("typeAliases"));// <5> 解析 <plugins /> 标签pluginElement(root.evalNode("plugins"));// <6> 解析 <objectFactory /> 标签objectFactoryElement(root.evalNode("objectFactory"));// <7> 解析 <objectWrapperFactory /> 标签objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));// <8> 解析 <reflectorFactory /> 标签reflectorFactoryElement(root.evalNode("reflectorFactory"));// <9> 赋值 <settings /> 到 Configuration 属性settingsElement(settings);// read it after objectFactory and objectWrapperFactory issue #631// <10> 解析 <environments /> 标签environmentsElement(root.evalNode("environments"));// <11> 解析 <databaseIdProvider /> 标签databaseIdProviderElement(root.evalNode("databaseIdProvider"));// <12> 解析 <typeHandlers /> 标签typeHandlerElement(root.evalNode("typeHandlers"));// <13> 解析 <mappers /> 标签mapperElement(root.evalNode("mappers"));} catch (Exception e) {throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);}
}
mapperElement方法里边会载 Mapper 映射配置文件
下一篇解析:Mapper 映射配置文件