Mybatis学习系列(十三):Mybatis整合spring

本文详细介绍了Mybatis整合Spring的过程,主要涉及SqlSessionFactory和SqlSession的初始化,以及mapper的解析和代理对象生成。通过mybatis-spring.jar,使用SqlSessionFactoryBean初始化SqlSessionFactory,SqlSessionTemplate实现SqlSession接口并创建代理对象。@MapperScan注解配合MapperScannerRegistrar生成MapperFactoryBean,由Spring管理,并通过getObject方法获取Mapper对象进行操作。
摘要由CSDN通过智能技术生成

Mybatis整合spring主要是通过mybatis-spring这个jar包来处理的,主要有两个重要的过程:

         1.SqlSessionFactory,SqlSession的初始化

         2.mapper的解析和生成代理对象

1.SqlSessionFactory,SqlSession的初始化,首先我们看一下项目中的使用配置

//这里通过SqlSessionFactoryBean去生成SqlSessionFactory对象
@Bean
    public SqlSessionFactory mcpSqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSourceMcp());
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/mcp/*.xml"));
        sessionFactoryBean.setTypeAliasesPackage("com.weimob.saas.marketing.scloud.service.impl.module.mcp.*");
        return sessionFactoryBean.getObject();
    }
	
	//这里通过SqlSessionFactory 获取SqlSessionTemplate 
	@Bean
    public SqlSessionTemplate mcpSqlSessionTemplate() throws Exception {
        SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(mcpSqlSessionFactory());
        return sqlSessionTemplate;
    }
	
    //这个是配置的事务处理器
	@Bean
    public DataSourceTransactionManager mcpDataSourceTransactionManager() {
        return new DataSourceTransactionManager(dataSourceMcp());
    }

1.1我们先看一下SqlSessionFactoryBean 这个类

实现了InitializingBean接口,我们看一下afterPropertiesSet这个方法:初始化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");
    //创建了sqlSessionFactory 对象
    this.sqlSessionFactory = buildSqlSessionFactory();
  }

//创建sqlSessionFactory 对象
//这里主要是通过配置的一些参数进行解析数据 生成Configuration 
//然后通过sqlSessionFactoryBuilder和Configuration 来创建SqlSessionFactory 
//这里解析跟我们在之前的文章里解析 普通mybatis的执解析是一样的,就不做详细解析了
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {

    Configuration configuration;

    XMLConfigBuilder xmlConfigBuilder = null;
    if (this.configuration != null) {
      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 = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);
      configuration = xmlConfigBuilder.getConfiguration();
    } else {
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration");
      }
      configuration = new Configuration();
      if (this.configurationProperties != null) {
        configuration.setVariables(this.configurationProperties);
      }
    }

    if (this.objectFactory != null) {
      configuration.setObjectFactory(this.objectFactory);
    }

    if (this.objectWrapperFactory != null) {
      configuration.setObjectWrapperFactory(this.objectWrapperFactory);
    }

    if (this.vfs != null) {
      configuration.setVfsImpl(this.vfs);
    }

    if (hasLength(this.typeAliasesPackage)) {
      String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
          ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
      for (String packageToScan : typeAliasPackageArray) {
        configuration.getTypeAliasRegistry().registerAliases(pa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值