美文网首页
mybatis多数据源配置并初始化数据库

mybatis多数据源配置并初始化数据库

作者: 江江的大猪 | 来源:发表于2021-05-20 10:46 被阅读0次
  • 多数据源场景不建议使用mybatis-spring-boot-starter,starter更适合单数据源配置
  • 多数据源场景建议全部自己设置,不要依赖springboot自动配置
  • 相关依赖
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis-spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
  • java config配置双数据源
@Configuration
public class DBConfig {
    /******************************************************************************************************
     ***************************************** metadata数据源配置 ***********************************************
     *****************************************************************************************************/
    @Bean
    private static HikariDataSource metadataDataSource(Config config) {
        HikariDataSource metadataDataSource = new HikariDataSource();
        metadataDataSource.setJdbcUrl(config.getMetadataDBUrl());
        metadataDataSource.setUsername(config.getMetadataDBUsername());
        metadataDataSource.setPassword(config.getMetadataDBPassword());
        metadataDataSource.setValidationTimeout(config.getValidationTimeout());
        metadataDataSource.setConnectionTimeout(config.getConnectionTimeout());
        metadataDataSource.setMaxLifetime(config.getMaxLifeTime());
        metadataDataSource.setMaximumPoolSize(config.getMaximumPoolSize());
        return metadataDataSource;
    }

    @Bean
    private static DataSourceInitializer metadataDataSourceInitializer(DataSource metadataDataSource, Config config) {
        DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
        dataSourceInitializer.setDataSource(metadataDataSource);
        ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
        resourceDatabasePopulator.addScript(config.getMetadataDBInit());
        dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
        return dataSourceInitializer;
    }

    @Bean
    private static DataSourceTransactionManager metadataTransactionManager(DataSource metadataDataSource) {
        return new DataSourceTransactionManager(metadataDataSource);
    }

    // mybatis需要等数据库初始化后再初始化
    @Bean
    @DependsOn("metadataDataSourceInitializer")
    private static SqlSessionFactory metadataSqlSessionFactory(DataSource metadataDataSource, Config config) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setDefaultStatementTimeout(config.getDefaultDBTimeout());
        factoryBean.setConfiguration(configuration);
        factoryBean.setDataSource(metadataDataSource);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(config.getMetadataMapperLocation()));
        return factoryBean.getObject();
    }

    @Bean
    private static MapperScannerConfigurer metadataMapperScanner() {
        MapperScannerConfigurer metadataMapperScanner = new MapperScannerConfigurer();
        metadataMapperScanner.setSqlSessionFactoryBeanName("metadataSqlSessionFactory");
        metadataMapperScanner.setBasePackage("xxx");
        return metadataMapperScanner;
    }

    /******************************************************************************************************
     ***************************************** console数据源配置 *******************************************
     *****************************************************************************************************/
    @Bean
    private static HikariDataSource consoleDataSource(Config config) {
        HikariDataSource consoleDataSource = new HikariDataSource();
        consoleDataSource.setJdbcUrl(config.getConsoleDBUrl());
        consoleDataSource.setUsername(config.getConsoleDBUsername());
        consoleDataSource.setPassword(config.getConsoleDBPassword());
        consoleDataSource.setValidationTimeout(config.getValidationTimeout());
        consoleDataSource.setConnectionTimeout(config.getConnectionTimeout());
        consoleDataSource.setMaxLifetime(config.getMaxLifeTime());
        consoleDataSource.setMaximumPoolSize(config.getMaximumPoolSize());
        return consoleDataSource;
    }

    @Bean
    private static DataSourceTransactionManager consoleTransactionManager(DataSource consoleDataSource) {
        return new DataSourceTransactionManager(consoleDataSource);
    }

    @Bean
    private static SqlSessionFactory consoleSqlSessionFactory(DataSource consoleDataSource, Config config) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setDefaultStatementTimeout(config.getDefaultDBTimeout());
        factoryBean.setConfiguration(configuration);
        factoryBean.setDataSource(consoleDataSource);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(config.getConsoleMapperLocation()));
        return factoryBean.getObject();
    }

    @Bean
    private static MapperScannerConfigurer consoleMapperScanner() {
        MapperScannerConfigurer consoleMapperScanner = new MapperScannerConfigurer();
        consoleMapperScanner.setSqlSessionFactoryBeanName("consoleSqlSessionFactory");
        consoleMapperScanner.setBasePackage("xxx");
        return consoleMapperScanner;
    }
}

相关文章

网友评论

      本文标题:mybatis多数据源配置并初始化数据库

      本文链接:https://www.haomeiwen.com/subject/qrymjltx.html