MyBatis温故而知新-底层运行原理

准备工作【参考资料】

public class MainClass {
   
  public static void main(String[] args) throws Exception {
   
    String resources = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resources);

    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession = sqlSessionFactory.openSession();

    Student student = sqlSession.selectOne("org.apache.ibatis.dao.StudentMapper.getStudent",1);
    System.out.println(student.toString());
    sqlSession.close();
  }
}

MyBatis是如何获取数据源的
这是我们mybatis-config.xml中配置数据库的4个关键属性,也就是看看MyBatis是怎么来解析这个配置文件块的。

<environments default="development">
   <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
      </dataSource>
    </environment>
 </environments>

数据源获取,我们从上面的代码片段中开始分析,在SqlSessionFactoryBuilder().build(inputStream) 方法中入手,这里看到实例了XMLConfigBuilder类。

SqlSessionFactoryBuilder

public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
   
    try {
   
      XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
      //parser.parse()方法返回Configuration对象,然后调用build(Configuration config)
      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.
      }
    }
}

public SqlSessionFactory build(Configuration config) {
   
    return new DefaultSqlSessionFactory(config);
}

XMLConfigBuilder#parse()

public Configuration parse() {
   
    if (parsed) {
   
      throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    }
    parsed = true;
    parseConfiguration(parser.evalNode("/configuration"));
    return configuration;
  }

  private void parseConfiguration(XNode root) {
   
    try {
   
      propertiesElement(root.evalNode("properties"));
      Properties settings = settingsAsProperties(root.evalNode("settings"));
      loadCustomVfs(settings);
      loadCustomLogImpl(settings);
      typeAliasesElement(root.evalNode("typeAliases"));
      pluginElement(root.evalNode("plugins"));
      objectFactoryElement(root.evalNode("objectFactory"));
      objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
      reflectorFactoryElement(root.evalNode("reflectorFactory"));
      settingsElement(settings);
      environmentsElement(root.evalNode("environments"));
      databaseIdProviderElement(root.evalNode("databaseIdProvider"));
      typeHandlerElement(root.evalNode("typeHandlers"));
      mapperElement(root.evalNode("mappers"));
    } catch (Exception e) {
   
      throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
    }
  }

看到parseConfiguration参数为XNode就知道这个方法里肯定是要解析xml节点了,可以debug查看下root参数的值,尝试debug后发现,其内容就是我们的配置文件mybatis-config.xml的文件内容。 接着我们在22行这里则看到了熟悉的environments节点,那就直接看下environmentsElement方法。

<configuration>
    <properties resource="db.properties"/>    
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>        
        <setting name="cacheEnabled" value="true"/>        
        <setting name="lazyLoadingEnabled" value="true"/>        
        <setting name="aggressiveLazyLoading" value="false"/>        
        <setting name="localCacheScope" value="SESSION"/>        
    </settings>
    <typeAliases>
        <typeAlias alias="Student" type="org.apache.ibatis.domain.Student"/>        
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>            
            <dataSource type="POOLED">
                <property name="driver"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值