首页 > 代码库 > mybatis源码分析(1)-----sqlSessionFactory创建

mybatis源码分析(1)-----sqlSessionFactory创建

1. 首先了解一下mybatis,包含核心jar ,以及spring相关jar.

<!-- Mybatis相关组件 -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.3.0</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.2.3</version>
</dependency>
  • 系列的文章也是mybatis 3.3.0和mybatis-spring1.2.3分析
  • mybatis jar包中只要包含mybaits的核心业务sqlsession,sqlsessionFactory,configuration
  • mybatis-spring 包中只要是和spring的相关集成,sqlSessionFactoryBean、transaction

 

2. 加载mybatis

本文以spring中加载mybatis为参考

<!-- 配置集成Mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:/config/SQLMapConfig.xml" />
    <property name="mapperLocations"
        value="classpath*:com/chinaap/*/**/infra/mybatis/*Mapper.xml" />
</bean>
  • sqlSessionFactory接口(重要接口)

  接口中有如下方法。

public interface SqlSessionFactory {
  SqlSession openSession();
  SqlSession openSession(boolean autoCommit);
  SqlSession openSession(Connection connection);
  SqlSession openSession(TransactionIsolationLevel level);
  SqlSession openSession(ExecutorType execType);
  SqlSession openSession(ExecutorType execType, boolean autoCommit);
  SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level);
  SqlSession openSession(ExecutorType execType, Connection connection);
  Configuration getConfiguration();
}

  接口的实现类

技术分享

  由sqlSessionFactoryBean产生的是默认的SqlSessionFactory(DefaultSqlSessionFactory)

 

  •  sqlSessionFactoryBean(mybatis-spring包下面的创建工厂的Bean,由spring 加载初始化)

mybatis源码分析(1)-----sqlSessionFactory创建