springboot mybatisplus 多数据源采坑

1:连接池依赖

com.zaxxer
HikariCP
2.7.9

2:配置类

spring:
  datasource:
    master:
      type: com.zaxxer.hikari.HikariDataSource
      default-auto-commit: false
      auto-commit: false
      maximum-pool-size: 100
      max-idle: 10
      max-wait: 10000
      min-idle: 5
      initial-size: 5
      validation-query: SELECT 1
      test-on-borrow: false
      test-while-idle: true
      time-between-eviction-runs-millis: 18800
      minEvictableIdleTimeMillis: 300000
      driver-class-name: com.mysql.jdbc.Driver
      jdbcUrl: jdbc:mysql://localhost:3306/master? 
            useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
      username: root
      password: 1234
  ds:
      type: com.zaxxer.hikari.HikariDataSource
      default-auto-commit: false
      auto-commit: false
      maximum-pool-size: 100
      max-idle: 10
      max-wait: 10000
      min-idle: 5
      initial-size: 5
      validation-query: SELECT 1
      test-on-borrow: false
      test-while-idle: true
      time-between-eviction-runs-millis: 18800
      minEvictableIdleTimeMillis: 300000
      driver-class-name: com.mysql.jdbc.Driver
      jdbcUrl: jdbc:mysql://localhost:3306/ds? 
              useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
      username: root
      password: 1234

3:java配置类
3.1:MasterMybatisConfig

   @MapperScan(basePackages = {"com.ssi.syc.dao"}, sqlSessionFactoryRef  = "masterSqlSessionFactory")
@Configuration
public class MasterMybatisConfig {

    @Primary
    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource masterDataSource() {
        return new HikariDataSource();
    }

    @Primary
    @Bean(name = "masterSqlSessionFactory")
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource datasource)
            throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        // sqlSessionFactory2使用的数据源为db2
        sqlSessionFactoryBean.setDataSource(datasource);
        sqlSessionFactoryBean
                .setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*Mapper.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    public Resource[] resolveMapperLocations() {
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        List<String> mapperLocations = new ArrayList<>();
        mapperLocations.add("classpath:mapper/syspower/**/*.xml");
        mapperLocations.add("classpath:mapper/master/**/*.xml");
        List<Resource> resources = new ArrayList();
        if (mapperLocations != null) {
            for (String mapperLocation : mapperLocations) {
                try {
                    Resource[] mappers = resourceResolver.getResources(mapperLocation);
                    resources.addAll(Arrays.asList(mappers));
                } catch (IOException e) {
                    // ignore
                }
            }
        }
        return resources.toArray(new Resource[resources.size()]);
    }

    @Primary
    @Bean(name = "masterTransactionManager")
    public DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource datasource) {
        return new DataSourceTransactionManager(datasource);
    }

3.2 DsMybatisConfig 配置类

@MapperScan(basePackages = "com.ssi.syc.dao1", sqlSessionFactoryRef = "dsSqlSessionFactory")
@Configuration
public class DsMybatisConfig {

    @Bean(name = "dsDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.ds")
    public DataSource masterDataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "dsSqlSessionFactory")
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("dsDataSource") DataSource datasource)
            throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        // sqlSessionFactory2使用的数据源为db2
        sqlSessionFactoryBean.setDataSource(datasource);
        sqlSessionFactoryBean
                .setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper2/*Mapper.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean(name = "dsTransactionManager")
    public DataSourceTransactionManager masterTransactionManager(@Qualifier("dsDataSource") DataSource datasource) {
        return new DataSourceTransactionManager(datasource);
    }

注意 @Primary 此注解

说明:
1:@Transactional(transactionManager = “masterTransactionManager”)
此事务上有注解 @Primary 此注解 ,事务回滚也可以回滚 此包下的事务com.ssi.syc.dao1
2:@Bean(name = “dsTransactionManager”)
此事务不能回滚 com.ssi.syc.dao此包下的事务
3:MybatisSqlSessionFactoryBean 创建sqlSession工厂时,由于是mybatis ,创建时如果采用SqlSessionFactoryBean 创建,无法加载mybatisplus默认mapper接口类,项目正常启动,但是当请求db的时候,出现org.apache.ibatis.binding.BindingException: Invalid bound statement not found 错误。

1赞