SpringBoot多數(shù)據(jù)源配置詳細教程

文章概要

  • 簡單多數(shù)據(jù)配置
  • JdbcTemplate 多數(shù)據(jù)源配置
  • mybatis多數(shù)據(jù)源配置
  • mybatis-plus多數(shù)據(jù)源配置

多數(shù)據(jù)源配置

首先是配置文件

  • 這里采用yml配置文件,其他類型配置文件同理
  • 我配置了兩個數(shù)據(jù)源,一個名字叫ds1數(shù)據(jù)源,一個名字叫ds2數(shù)據(jù)源,如果你想配置更多的數(shù)據(jù)源,繼續(xù)加就行了
spring:
 # 數(shù)據(jù)源配置
  datasource:
    ds1: #數(shù)據(jù)源1
      driver-class-name: com.mysql.jdbc.Driver # mysql的驅(qū)動你可以配置別的關(guān)系型數(shù)據(jù)庫
      url: jdbc:mysql://ip:3306/db1 #數(shù)據(jù)源地址
      username: root # 用戶名
      password: root # 密碼
    ds2: # 數(shù)據(jù)源2
      driver-class-name: com.mysql.jdbc.Driver # mysql的驅(qū)動你可以配置別的關(guān)系型數(shù)據(jù)庫
      url: jdbc:mysql://ip:3307/db2#數(shù)據(jù)源地址
      username: root # 用戶名
      password: root # 密碼

多數(shù)據(jù)源配置

增加一個Springboot的配置類

/**
 * 多數(shù)據(jù)源配置
 */
@Configuration
public class DataSourceConfig {

    //主數(shù)據(jù)源配置 ds1數(shù)據(jù)源
    @Primary
    @Bean(name = "ds1DataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.ds1")
    public DataSourceProperties ds1DataSourceProperties() {
        return new DataSourceProperties();
    }

    //主數(shù)據(jù)源 ds1數(shù)據(jù)源
    @Primary
    @Bean(name = "ds1DataSource")
    public DataSource ds1DataSource(@Qualifier("ds1DataSourceProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    //第二個ds2數(shù)據(jù)源配置
    @Bean(name = "ds2DataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.ds2")
    public DataSourceProperties ds2DataSourceProperties() {
        return new DataSourceProperties();
    }

    //第二個ds2數(shù)據(jù)源
    @Bean("ds2DataSource")
    public DataSource ds2DataSource(@Qualifier("ds2DataSourceProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

}

JdbcTemplate多數(shù)據(jù)源配置

增加一個Springboot配置類

/**
 * JdbcTemplate多數(shù)據(jù)源配置
 * 依賴于數(shù)據(jù)源配置
 *
 * @see DataSourceConfig
 */
@Configuration
public class JdbcTemplateDataSourceConfig {

    //JdbcTemplate主數(shù)據(jù)源ds1數(shù)據(jù)源
    @Primary
    @Bean(name = "ds1JdbcTemplate")
    public JdbcTemplate ds1JdbcTemplate(@Qualifier("ds1DataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    //JdbcTemplate第二個ds2數(shù)據(jù)源
    @Bean(name = "ds2JdbcTemplate")
    public JdbcTemplate ds2JdbcTemplate(@Qualifier("ds2DataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

mybatis 多數(shù)據(jù)源配置

增加一個SpringBoot配置類
mybatis多數(shù)據(jù)源的原理是根據(jù)不同包,調(diào)用不同的數(shù)據(jù)源,你只需要把你的mapper.java和mapper.xml(我喜歡叫dao.java和dao.xml)寫在某個package中,springboot自動幫你實現(xiàn)數(shù)據(jù)源切換
核心代碼就這句
@MapperScan(basePackages ="com.web.ds2.**.dao", sqlSessionTemplateRef = "ds2SqlSessionTemplate")
用來指定包掃描指定sqlSessionTemplateRef
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/web/ds2/**/*.xml"));
用來指定mapper.xml的路徑

詳細配置代碼如下

/**
 * Mybatis主數(shù)據(jù)源ds1配置
 * 多數(shù)據(jù)源配置依賴數(shù)據(jù)源配置
 * @see  DataSourceConfig
 */
@Configuration
@MapperScan(basePackages ="com.web.ds1.**.dao", sqlSessionTemplateRef  = "ds1SqlSessionTemplate")
public class MybatisPlusConfig4ds1 {

    //主數(shù)據(jù)源 ds1數(shù)據(jù)源
    @Primary
    @Bean("ds1SqlSessionFactory")
    public SqlSessionFactory ds1SqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
                        getResources("classpath*:com/web/ds1/**/*.xml"));  
        return sqlSessionFactory.getObject();
    }

    @Primary
    @Bean(name = "ds1TransactionManager")
    public DataSourceTransactionManager ds1TransactionManager(@Qualifier("ds1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean(name = "ds1SqlSessionTemplate")
    public SqlSessionTemplate ds1SqlSessionTemplate(@Qualifier("ds1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
/**
 * Mybatis  第二個ds2數(shù)據(jù)源配置
 * 多數(shù)據(jù)源配置依賴數(shù)據(jù)源配置
 * @see  DataSourceConfig
 */
@Configuration
@MapperScan(basePackages ="com.web.ds2.**.dao", sqlSessionTemplateRef  = "ds2SqlSessionTemplate")
public class MybatisPlusConfig4ds2 {

    //ds2數(shù)據(jù)源
    @Bean("ds2SqlSessionFactory")
    public SqlSessionFactory ds2SqlSessionFactory(@Qualifier("ds2DataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
                getResources("classpath*:com/web/ds2/**/*.xml"));
        return sqlSessionFactory.getObject();
    }

//事務支持
    @Bean(name = "ds2TransactionManager")
    public DataSourceTransactionManager ds2TransactionManager(@Qualifier("ds2DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "ds2SqlSessionTemplate")
    public SqlSessionTemplate ds2SqlSessionTemplate(@Qualifier("ds2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

mybatis-plus 多數(shù)據(jù)源配置

mybatis-plus是mybatis的增強版,只增加,不影響。也就是說使用mybatis-plus兼容原來所有的mybatis代碼和配置。然后又做了很多增加和簡化使用,具體看官網(wǎng)教程https://mybatis.plus/

相對于mybatis的多數(shù)據(jù)源配置就是改了下 SqlSessionFactory
核心代碼就是修改mybatis為mybatis-plus,如下

    @Bean("ds2SqlSessionFactory")
    public SqlSessionFactory ds2SqlSessionFactory(@Qualifier("ds2DataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        sqlSessionFactory.setConfiguration(configuration);
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
                getResources("classpath*:com/web/ds2/**/*.xml"));
        sqlSessionFactory.setPlugins(new Interceptor[]{
                new PaginationInterceptor(),
                new PerformanceInterceptor()
//                        .setFormat(true),
        });
        sqlSessionFactory.setGlobalConfig(new GlobalConfig().setBanner(false));
        return sqlSessionFactory.getObject();
    }

全部配置代碼如下

/**
 * Mybatis-plus ds2數(shù)據(jù)源配置
 * 多數(shù)據(jù)源配置依賴數(shù)據(jù)源配置
 * @see  DataSourceConfig
 */
@Configuration
@MapperScan(basePackages ="com.web.ds2.**.dao", sqlSessionTemplateRef  = "ds2SqlSessionTemplate")
public class MybatisPlusConfig4ds2{

    //ds2數(shù)據(jù)源
    @Bean("ds2SqlSessionFactory")
    public SqlSessionFactory ds2SqlSessionFactory(@Qualifier("ds2DataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        sqlSessionFactory.setConfiguration(configuration);
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
                getResources("classpath*:com/web/ds2/**/*.xml"));
        sqlSessionFactory.setPlugins(new Interceptor[]{
                new PaginationInterceptor(),
                new PerformanceInterceptor()
//                        .setFormat(true),
        });
        sqlSessionFactory.setGlobalConfig(new GlobalConfig().setBanner(false));
        return sqlSessionFactory.getObject();
    }

    @Bean(name = "ds2TransactionManager")
    public DataSourceTransactionManager ds2TransactionManager(@Qualifier("ds2DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "ds2SqlSessionTemplate")
    public SqlSessionTemplate ds2SqlSessionTemplate(@Qualifier("ds2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
/**
 * Mybatis-plus 主數(shù)據(jù)源ds1配置
 * 多數(shù)據(jù)源配置依賴數(shù)據(jù)源配置
 * @see  DataSourceConfig
 */
@Configuration
@MapperScan(basePackages ="com.web.ds1.**.dao", sqlSessionTemplateRef  = "ds1SqlSessionTemplate")
public class MybatisPlusConfig4ds1 {

    //主數(shù)據(jù)源 ds1數(shù)據(jù)源
    @Primary
    @Bean("ds1SqlSessionFactory")
    public SqlSessionFactory ds1SqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        sqlSessionFactory.setConfiguration(configuration);
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
                        getResources("classpath*:com/ds1/web/ds1/**/*.xml"));
        sqlSessionFactory.setPlugins(new Interceptor[]{
                new PaginationInterceptor(),
                new PerformanceInterceptor()
//                        .setFormat(true),
        });
        sqlSessionFactory.setGlobalConfig(new GlobalConfig().setBanner(false));
        return sqlSessionFactory.getObject();
    }

    @Primary
    @Bean(name = "ds1TransactionManager")
    public DataSourceTransactionManager ds1TransactionManager(@Qualifier("ds1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean(name = "ds1SqlSessionTemplate")
    public SqlSessionTemplate ds1SqlSessionTemplate(@Qualifier("ds1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

其他的多數(shù)據(jù)配置請看官你自己舉一反三

推廣 我的技術(shù)群 317896269

點擊鏈接加入群聊【數(shù)據(jù)爬取技術(shù)群】:https://jq.qq.com/?_wv=1027&k=5ZRxI17
重要的事情說五遍:
317896269
317896269
317896269
317896269
317896269

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容