首頁 >Java >java教程 >springboot專案配置兩個資料來源的方法

springboot專案配置兩個資料來源的方法

不言
不言原創
2018-09-20 14:50:115471瀏覽

這篇文章帶給大家的內容是關於springboot專案配置兩個資料來源的方法,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

本文主要介紹如何在一個springboot專案中配置兩個資料來源(mysql和oracle);

1、引進相關依賴

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<!--oracle 驱动 -->
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.1.0.7.0</version>
</dependency>

當ojdbc驅動套件版本過低,會報如下圖所示錯誤,即驅動jar與資料庫版本不相容:

#2、於applicationContext.yml中配置資料源連接參數:

Spring:
        datasource:
            base:
                 driver-class-name: com.mysql.jdbc.Driver
                 jdbc-url: ${base.db.url}
                 username: ${base.db.username}
                 password: ${base.db.password}
            oa:
                 driver-class-name: oracle.jdbc.driver.OracleDriver
                 jdbc-url: ${oa.db.urL}
                 username: ${oa.db.username}
                 password: ${oa.db.password}
            hikari:
                 max-lifetime: 60000
                 login-timeout: 5
                 validation-timeout: 3000
                 connection-timeout: 60000
                 idle-timeout: 60000

3、多資料來源設定文件,讀取對應連接參數

#
Package com.**.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;

public class MultiDataSourceConfig {

    @Bean (name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @Primary //定义主数据源
    @ConfigurationProperties (prefix = "spring.datasource.Base")
    public DataSource primaryDataSource () { return DataSourceBuilder.create().build (); }

    @Bean (name = "secondaryDataSource")
    @Qualifier ("secondaryDataSource")
    @ConfigurationProperties (prefix = "spring.datasource.oa")
    public DataSource secondaryDataSource () { return DataSourceBuilder.create().build (); }

}

3、主資料來源配置,指定掃描對應mapper檔案以及對應xml文件,在使用mapper套件下的mapper檔案時會自動使用對應sql會話

Package com.**.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqLSessionFactoryBean;
import org.mybatis.spring.SqLSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframeworkcore.io.support.PathMatchingResourcePatternResolver;
import org.spr ingframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;

@Configuration
@MapperScan (basePackages = "com.**.mapper", sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class SqlSessionTemplate1 {

    @Bean (name = "primarySqlSessionFactory")
    @Primary
    public SqlSessionFactory primarySqlSessionFactory  (@Qualifier("primaryDataSource") DataSource dataSource)
            throws Exception{
        SqLSessionFactoryBean bean = new SqlSessionFactoryBean ();
        bean.setDataSource (dataSource);
        bean.setMapperLocations (new PathMatchingResourcePatternResolver().getResources (locationPattern: "classpath: mybatis/mapper/*. XmL"));
        return bean.getObject();
    }

    /**
     * 配置声明式事务管理器
     */
    @Bean (name = "primaryTransactionManager")
    @Primary
    public PlatformTransactionManager primaryTransactionManager  (@Qualifier("primaryDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager  (dataSource);
    }

    @Bean (name = "primarySqlSessionTemplate")
    @Primary
    public SqlSessionTemplatel primarySqlSessionTemplate(@Qualifier("primarySqlSesionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplatel(sqlSessionFactory);
    }
}

4、第二個資料來源配置,指定掃描對應mapper檔案以及對應xml文件,在使用mapper套件下的mapper檔案時會自動使用對應sql會話

Package com.**.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqLSessionFactoryBean;
import org.mybatis.spring.SqLSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.secondary;
import org.springframeworkcore.io.support.PathMatchingResourcePatternResolver;
import org.spr ingframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan (basePackages = "com.**.oraclemapper", sqlSessionTemplateRef = "secondarySqlSessionTemplate")
public class SqlSessionTemplate2 {
    @Bean (name = "secondarySqlSessionFactory")
    public SqlSessionFactory secondarySqlSessionFactory  (@Qualifier("secondaryDataSource") DataSource dataSource)
            throws Exception{
        SqLSessionFactoryBean bean = new SqlSessionFactoryBean ();
        bean.setDataSource (dataSource);
        bean.setMapperLocations (new PathMatchingResourcePatternResolver().getResources (locationPattern: "classpath: mybatis/oraclemapper/*. XmL"));
        return bean.getObject();
    }

    /**
     * 配置声明式事务管理器
     */
    @Bean (name = "secondaryTransactionManager")
    public PlatformTransactionManager secondaryTransactionManager  (@Qualifier("secondaryDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager  (dataSource);
    }

    @Bean (name = "secondarySqlSessionTemplate")
    public SqlSessionTemplatel secondarySqlSessionTemplate(@Qualifier("secondarySqlSesionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplatel(sqlSessionFactory);
    }
}

至此,在service層可以像單一資料來源一樣使用了。

以上是springboot專案配置兩個資料來源的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn