首頁 > Java > java教程 > 主體

JAVA開發之springBoot2.0建置雙資料來源

无忌哥哥
發布: 2018-07-20 11:32:06
原創
2505 人瀏覽過

最近因為專案需要,需要在程式中同時存取不同的資料庫。之前使用springboot註解寫入程式碼確實方便快捷,但是如果需要自己需改其中的一些東西來方便自己使用,改起來有點煩,不知道從哪裡入手。寫個這個記錄下。

首先是pom.xml,都是通用的,這裡就不貼出來了。

其次是application.properties配置,筆者因為不同的環境,所以新建了兩個application-dev.properties和application-pro.properties設定檔。

 application.properties:指定目前使用該環境的配置.

spring.profiles.active=dev
登入後複製
application-dev.properties的配置(application-pro.properties类似dev的配置)
登入後複製
#datasource1
spring.datasource.username=**
spring.datasource.password=*****
spring.datasource.jdbc-url=jdbc:mysql://***************
useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#datasource2
spring.datasource.ds1.username=***
spring.datasource.ds1.password=******
spring.datasource.ds1.jdbc-url=jdbc:mysql://***************
useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.ds1.driver-class-name=com.mysql.jdbc.Drive
登入後複製

不同資料來源的MyBatis (Config1)

@Configuration
@MapperScan(basePackages = "com.pet.petgame2.mapper.dao", sqlSessionFactoryRef = "petgame2SessionFactory")
public class Config1 {

    @Bean(name = "petgame2")
    @ConfigurationProperties(prefix = "spring.datasource")
    @Primary
    public DataSource dataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "petgame2SessionFactory")
    @Primary
    public SqlSessionFactory sessionFactory(@Qualifier("petgame2") DataSource dataSource) throws Exception{
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:Mapper/pet2Mapper/*.xml"));
        return factoryBean.getObject();
    }

    @Bean(name = "petgame2TransactionManager")
    @Primary
    public DataSourceTransactionManager transactionManager(@Qualifier("petgame2") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }
}
登入後複製

不同資料來源的MyBatis (Config2) 

#
@Configuration
@MapperScan(basePackages = "com.pet.petgame2.mapper.dao2", sqlSessionFactoryRef = "petgamesqlSessionFactory")
public class Config2 {

    @Bean(name = "petgame")
    @ConfigurationProperties(prefix = "spring.datasource.ds1")
    public DataSource dataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "petgamesqlSessionFactory")
    public SqlSessionFactory sessionFactory(@Qualifier("petgame") DataSource dataSource) throws Exception{
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:Mapper/petMapper/*.xml"));
        return factoryBean.getObject();
    }

    @Bean(name = "petgameTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("petgame") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }
}
登入後複製

@MapperScan註解會自動掃描mapper的介面文件, factoryBean.setMapperLocations會掃描mapper介面文件對應的Mapper.XML文件,注意這兩個的路徑不要寫錯。 @Primary設定預設的一個資料庫連結配置,必須指定其中一個資料來源為預設。

*注意:如果使用了springboot2.0自帶的Hikari作為連接池,需要注意properties中的

spring.datasource.url改为spring.datasource.jdbc-url
登入後複製

或修改config中DataSource為DataSourceProperties,如下

@Bean(name = "petgame2DataSourceProperties")
    @Qualifier("petgame2DataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource")
    @Primary
    public DataSourceProperties properties(){
        return new DataSourceProperties();
    }

    @Bean(name = "petgame2")
    @ConfigurationProperties(prefix = "spring.datasource")
    @Primary
    public DataSource dataSource(){
//        return DataSourceBuilder.create().build();
        return properties().initializeDataSourceBuilder().build();
    }
登入後複製

以上是JAVA開發之springBoot2.0建置雙資料來源的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!