content: package com.george.springcloud.alibaba.config;
  
  import com.alibaba.druid.pool.DruidDataSource;
  import io.seata.rm.datasource.DataSourceProxy;
  import org.apache.ibatis.session.SqlSessionFactory;
  import org.mybatis.spring.SqlSessionFactoryBean;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.boot.context.properties.ConfigurationProperties;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Primary;
  import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  import org.springframework.core.io.support.ResourcePatternResolver;
  
  import javax.sql.DataSource;
  
  /**
   * 使用seata对数据源进行代理
   *
   * @author Yang Hao
   * @date 2020-09-27 11:49
   */
  @Configuration
  public class DataSourceProxyConfig {
  
      @Value("${mybatis.mapper-locations}")
      private String mapperLocations;
  
      @Bean
      @ConfigurationProperties(prefix = "spring.datasource")
      public DataSource druidDataSource() {
          return new DruidDataSource();
      }
  
      @Primary
      @Bean("dataSource")
      public DataSourceProxy dataSourceProxy(DataSource druidDataSource) {
          return new DataSourceProxy(druidDataSource);
      }
  
      @Bean(name = "sqlSessionFactory")
      public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
          SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
          bean.setDataSource(dataSourceProxy);
          ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
          bean.setMapperLocations(resolver.getResources(mapperLocations));
  
          SqlSessionFactory factory;
          try {
              factory = bean.getObject();
          } catch (Exception e) {
              throw new RuntimeException(e);
          }
          return factory;
      }
  
  }
  