Mybatis源码学习第十课---Mybatis与spring集成源码

本文详细探讨了Mybatis与Spring的集成配置,包括SqlSessionFactoryBean的源码分析,解释了如何配置数据源、扫描Mapper接口。同时,深入解析了SqlSessionTemplate的工作原理,以及MapperFactoryBean如何创建和管理Mapper接口的动态代理对象,最后总结了Mybatis-Spring集成的关键点。
摘要由CSDN通过智能技术生成

目录

一 、集成配置

二 、SqlSessionFactoryBean源码分析

三   SqlSessionTemplate 

四 、MapperFactoryBean

4.1 MapperFactoryBean测试

4.2 MapperFactoryBean源码解析

2.2.1 这里解决了一个重要问题

五、MapperScannerConfigurer

六.总结

参考


一 、集成配置

   在pom.xml文件中添加MyBatis-Spring的依赖

<!-- mybatis与spring对接依赖 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.3</version>
		</dependency>

  配置SqlSessionFactoryBean

     applicationContext.xml进行配置。

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="typeAliasesPackage" value="com.enjoylearning.mybatis.entity" />
		<property name="mapperLocations" value="classpath:sqlmapper/*.xml" />
		<property name="configLocation" value="mybatis-config.xml"/>
	</bean>

在MyBatis-Spring中,SqlSessiongFacotyBean是用于创建SqlSessionFactory的。

  • dataSource:用于配置数据源,该属性为必选项,必须通过这个属性配置数据源
  • mapperLocations:配置SqlSessionFactoryBean扫描XML映射文件的路径,可以使用Ant风格的路径进行配置
  • configLocation:用于配置mybatis config XML的路径,除了数据源外,对MyBatis的各种配置仍然可以通过这种方式进行,并且配置MyBatis settings时只能使用这种方式。但配置文件中任意环境、数据源和MyBatis的任务管理器都会被忽略。
  • typeAliasesPackage:配置包中类的别名,配置后,包中的类在XML映射文件中使用时可以省略包名部分,直接使用类名。这个配置不支持Ant风格的路径,当需要配置多个包路径时可以使用分号或逗号进行分隔。

配置MapperScannerConfigurer或者扫描包

 <bean id="tUserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="com.enjoylearning.mybatis.mapper.TUserMapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean> 
	
	<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
	 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.enjoylearning.mybatis.mapper" />
	</bean> 

通过MapperScannerConfigurer类自动扫描所有的Mapper接口,使用时可以直接注入接口。
MapperScannerConfigurer中常配置以下两个属性:

  • basePackage:用于配置基本的包路径。可以使用分号或逗号作为分隔符设置多于一个的包路径,每个映射器将会在指定的包路径中递归地被搜索到。
  • annotationClass:用于过滤被扫描的接口,如果设置了该属性,那么MyBatis的接口只有包含注解才会被扫描进去。

二 、SqlSessionFactoryBean源码分析

    MyBatis 初始化过程时提到,SqlSessionFactoryBuilder会通过XMLConfigBuilder 等对象读取 mybatis-config.xml 配置文件以及映射配置信息,得到 Configuration 对象,然后创建 Sq!SessionFactory 对象。而在与 spring 集成时, MyBatis 中的SqlSessionFactory 对象则是由 Sq!SqlSessionFactoryBuilder创建的。

实现FactoryBean接口的getObject方法:

public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {....}

 /**
   * 
   * 将SqlSessionFactory对象注入spring容器
   * {@inheritDoc}
   */
  @Override
  public SqlSessionFactory getObject() throws Exception {
    if (this.sqlSessionFactory == null) {
      afterPropertiesSet();
    }

    return this.sqlSessionFactory;
  }

SqlSessionFactoryBean实现InitializingBean接口,需要实现其afterPropertiesSet():

 @Override
  //在spring容器中创建全局唯一的sqlSessionFactory
  public void afterPropertiesSet() throws Exception {
    notNull(dataSource, "Property 'dataSource' is required");
    notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");
    state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
              "Property 'configuration' and 'configLocation' can not specified with together");

    this.sqlSessionFactory = buildSqlSessionFactory();
  }

    SqlSessionFactoryBean是如何创建 SqlSessionFactory对象的,该功能是在SqlSessionFactoryBean.buildSqlSessionFactory()方法中实现的,其中涉及使用 XMLConfigBuiIder 创建 Configuration 对象、对 Configuration 对象进行配置、使用 XMLMapperBuilder 解析映射配。核心是buildSqlSessionFactory:

protected SqlSessionFactory buildSqlSessionFactory() throws IOException {

    Configuration configuration;

    XMLConfigBuilder xmlConfigBuilder = null;
    if (this.configuration != null) {//如果configuration不为空,则使用该对象,并对其进行配置
      configuration = this.configuration;
      if (configuration.getVariables() == null) {
        configuration.setVariables(this.configurationProperties);
      } else if (this.configurationProperties != null) {
        configuration.getVariables().putAll(this.configurationProperties);
      }
    } else if (this.configLocation != null) {//创建xmlConfigBuilder,读取mybatis的核心配置文件
      xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);
      configuration = xmlConfigBuilder.getConf
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值