Heim > Java > javaLernprogramm > Hauptteil

So konfigurieren Sie zwei Datenquellen im Springboot-Projekt

不言
Freigeben: 2018-09-20 14:50:11
Original
5403 Leute haben es durchsucht

Der Inhalt dieses Artikels befasst sich mit der Konfiguration zweier Datenquellen im Springboot-Projekt. Ich hoffe, dass er für Sie hilfreich ist.

In diesem Artikel wird hauptsächlich die Konfiguration von zwei Datenquellen (MySQL und Oracle) in einem Springboot-Projekt vorgestellt.

Einführung verwandter Abhängigkeiten

<!-- 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>
Nach dem Login kopieren

Wenn die Version des ojdbc-Treiberpakets zu niedrig ist, wird ein Fehler wie unten gezeigt gemeldet, d. h. das Treiber-JAR ist nicht mit der Datenbankversion kompatibel:

2. Konfigurieren Sie Datenquellen-Verbindungsparameter in 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
Nach dem Login kopieren

3. Mehrere Datenquellen-Konfigurationsdateien, lesen Sie die entsprechenden Verbindungsparameter

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 (); }

}
Nach dem Login kopieren

3. Geben Sie an, dass die entsprechende Mapper-Datei und die entsprechende XML-Datei gescannt werden sollen Die entsprechende SQL-Sitzung wird automatisch verwendet

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);
    }
}
Nach dem Login kopieren

4. Die zweite Datenquellenkonfiguration legt das Scannen der entsprechenden Mapper-Datei und der entsprechenden XML-Datei fest Datei unter dem Mapper-Paket, die entsprechende SQL-Sitzung wird automatisch verwendet

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);
    }
}
Nach dem Login kopieren

An diesem Punkt kann die Serviceschicht wie eine einzelne Datenquelle verwendet werden.

Das obige ist der detaillierte Inhalt vonSo konfigurieren Sie zwei Datenquellen im Springboot-Projekt. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!