SpringBoot2.1.9 多数据源Mybatis—JDBC配置

一、配置文件
pom.xm

 
   org.mybatis.spring.boot
   mybatis-spring-boot-starter


   mysql
   mysql-connector-java
   5.1.48
   runtime

application.yml

spring:
  application:
    name: double-datasource-mybatis-jdbc
  profiles:
    active: dev
 
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
 
  server:
    port: 8008

mybatis:
  #mapper-locations: classpath*:mapper/*.xml
  type-aliases-package: com.mk.entity.*  # 注意:对应实体类的路径
  configuration:
      map-underscore-to-camel-case: true

sys:
  one-mybatis:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.1.2:3307/testdb01?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
      username: root
      password: admin123
      druid:
        initial-size: 10
        max-active: 100
        min-idle: 10
        max-wait: 60000
        pool-prepared-statements: true
        max-pool-prepared-statement-per-connection-size: 20
        time-between-eviction-runs-millis: 60000
        min-evictable-idle-time-millis: 300000
        validation-query: SELECT 1 FROM DUAL
        test-while-idle: true
        test-on-borrow: false
        test-on-return: false
        filter:
          stat:
            log-slow-sql: true
            slow-sql-millis: 1000
            merge-sql: true
            enabled: true
          wall:
            config:
              multi-statement-allow: true
        stat-view-servlet:
          enabled: false
  two-jdbc:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.1.2:3307/testdb02?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
      username: root
      password: admin123
      druid:
        initial-size: 10
        max-active: 100
        min-idle: 10
        max-wait: 60000
        pool-prepared-statements: true
        max-pool-prepared-statement-per-connection-size: 20
        time-between-eviction-runs-millis: 60000
        min-evictable-idle-time-millis: 300000
        validation-query: SELECT 1 FROM DUAL
        test-while-idle: true
        test-on-borrow: false
        test-on-return: false
        filter:
          stat:
            log-slow-sql: true
            slow-sql-millis: 1000
            merge-sql: true
            enabled: true
          wall:
            config:
              multi-statement-allow: true
        stat-view-servlet:
          enabled: false

二、配置Configuration Bean

(1)第一个Mybatis数据源配置(主)

public class MybatisDataSourceConfig {

    @Primary
    @Bean
    @ConfigurationProperties(prefix = "sys.one-mybatis.datasource")
    public DataSourceProperties oneDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Primary
    @Bean
    public DataSource oneDataSource(@Qualifier("oneDataSourceProperties") DataSourceProperties dataSourceProperties) {

        DataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().build();
        return dataSource;
    }

    @Primary
    @Bean
    @ConfigurationProperties(prefix = "mybatis")
    public MybatisProperties oneMybatisProperties(){
        return new MybatisProperties();
    }

    @Primary
    @Bean
    public SqlSessionFactoryBean oneSqlSessionFactoryBean(@Qualifier("oneDataSource") DataSource dataSource,
                                                          @Qualifier("oneMybatisProperties")MybatisProperties mybatisProperties){
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(mybatisProperties.resolveMapperLocations());
        bean.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());
        bean.setConfigurationProperties(mybatisProperties.getConfigurationProperties());
        bean.setConfiguration(mybatisProperties.getConfiguration());
        bean.setConfigLocation(Optional.ofNullable(mybatisProperties.getConfigLocation()).map(location->{
            try {
                ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
                return resourceResolver.getResource(location);
            } catch (Exception var3) {
                return null;
            }
        }).orElse(null));
        return bean;
    }


    @Primary
    @Bean
    public DataSourceTransactionManager oneTransactionManager(@Qualifier("oneDataSource") DataSource dataSource) {

        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean
    public SqlSessionTemplate oneSqlSessionTemplate(@Qualifier("oneSqlSessionFactoryBean") SqlSessionFactoryBean sqlSessionFactoryBean) throws Exception{
        return new SqlSessionTemplate(sqlSessionFactoryBean.getObject());
    }
 
}

(2)第二个jdbc配置

public class JdbcDataSourceConfig {

    @Bean
    @ConfigurationProperties(prefix = "sys.jdbc-two")
    public DataSourceProperties jdbcDataSourceProperties(){
        return new DataSourceProperties();
    }

    @Bean
    public DataSource jdbcDataSource(@Autowired @Qualifier("jdbcDataSourceProperties") DataSourceProperties dataSourceProperties){
        DataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().build();
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(@Autowired @Qualifier("jdbcDataSource") DataSource dataSource){
        
        return new JdbcTemplate(dataSource);
    }

    @Bean
    public DataSourceTransactionManager twoTransactionManager(@Autowired @Qualifier("jdbcDataSource") DataSource dataSource) {

        return new DataSourceTransactionManager(dataSource);
    }
}

(3)导入配置

@Configuration
@Import({MybatisDataSourceConfig.class, JdbcDataSourceConfig.class})
public class DataSourceConfig {
 
}

注意:去掉默认自动配置

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class, MybatisAutoConfiguration.class})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

 

你可能感兴趣的:(Spring,Boot)