亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

sping boot 配置多數據源

發布時間:2020-06-26 10:18:19 來源:網絡 閱讀:1296 作者:飛鳥如林 欄目:開發技術

spring boot 本身自帶對數據源的支持。 但在項目開發過程中,存在這種情況需要用到多數據源,用spring boot 自帶的數據源就不是那么方便了。這里有個實例實現spring boot 完成多數據源的配置。

項目的目錄結構是這樣的。
sping boot 配置多數據源

下面看看數據源配置

// 主數據源配置
package com.tansun.beanconfig.datasourse;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
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.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * 注:主程序使用數據源
 * @author kangx
 *
 */
@Configuration
@EnableAutoConfiguration
@MapperScan(basePackages = MainDSConfig.PACKAGE, sqlSessionTemplateRef = "masterSqlSessionTemplate")
public class MainDSConfig {
    // 精確到 master 目錄,以便跟其他數據源隔離,不同數據源掃描的mapper
        // 文件地址是不一樣的,如果放到統一路徑啟動會報生成bean沖突。
    static final String PACKAGE = "com.tansun.model.*.mapper";
    static final String MAPPER_LOCATION = "classpath:com/tansun/model/*/master/*.xml";

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String user;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driver-class-name}")
    private String driverClass;

    /**
     * 注:主數據源加@Primary 注解 表示當系統有多個datasource 滿足條件,
     * 默認使用@Primary標記的數據源
     * @return
     */
    @Bean(name = "masterDataSource")
    @Primary
    public DataSource masterDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setDbType("oracle");
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean(name = "masterTransactionManager")
    @Primary
    public DataSourceTransactionManager masterTransactionManager() {
        return new DataSourceTransactionManager(masterDataSource());
    }

    @Bean(name = "masterSqlSessionFactory")
    @Primary
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource,
            @Qualifier("databaseIdProvider") DatabaseIdProvider databaseIdProvider)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        sessionFactory.setDatabaseIdProvider(databaseIdProvider);
        return sessionFactory.getObject();
    }

    @Bean(name = "masterSqlSessionTemplate")
    public SqlSessionTemplate outerSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean("databaseIdProvider")
    public DatabaseIdProvider getDatabaseIdProvider() {
        DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        Properties pro = new Properties();
        pro.setProperty("Oracle", "oracle");
        pro.setProperty("MySQL", "mysql");
        databaseIdProvider.setProperties(pro);
        return databaseIdProvider;
    }

}

// 從數據源配置

package com.tansun.beanconfig.datasourse;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
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.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;
/**
 * 注:定時任務使用數據源
 * @author kangx
 */
@Configuration
@EnableAutoConfiguration
// import 表示引用主數據源的配置bean
@Import(value = { MainDSConfig.class })
@MapperScan(basePackages = RunTaskDSConfig.PACKAGE, sqlSessionTemplateRef = "runtaskSqlSessionTemplate")
public class RunTaskDSConfig {

    // 精確到 master 目錄,以便跟其他數據源隔離--這兒掃描的地址和主數據地址//不同
    static final String PACKAGE = "com.tansun.taskmodel.*.mapper";
    static final String MAPPER_LOCATION = "classpath:com/tansun/taskmodel/*/master/*.xml";

    @Value("${runtask.datasource.url}")
    private String url;

    @Value("${runtask.datasource.username}")
    private String user;

    @Value("${runtask.datasource.password}")
    private String password;

    @Value("${runtask.datasource.driverClassName}")
    private String driverClass;

    @Bean(name = "runtaskDataSource")
    public DataSource masterDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setDbType("mysql");
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean(name = "runtaskTransactionManager")
    public DataSourceTransactionManager masterTransactionManager() {
        return new DataSourceTransactionManager(masterDataSource());
    }

    @Bean(name = "runtaskSqlSessionFactory")
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("runtaskDataSource") DataSource masterDataSource,
            @Qualifier("databaseIdProvider") DatabaseIdProvider databaseIdProvider)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        sessionFactory.setDatabaseIdProvider(databaseIdProvider);
        return sessionFactory.getObject();
    }

    @Bean(name = "runtaskSqlSessionTemplate")
    public SqlSessionTemplate outerSqlSessionTemplate(@Qualifier("runtaskSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean("runtaskDatabaseIdProvider")
    public DatabaseIdProvider getDatabaseIdProvider() {
        DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        Properties pro = new Properties();
        pro.setProperty("Oracle", "oracle");
        pro.setProperty("MySQL", "mysql");
        databaseIdProvider.setProperties(pro);
        return databaseIdProvider;
    }
}

注 :
1主數據源配置 bean 上加@Primary標簽,表示當找多個符合的匹配時,使用由 primary 標記的bean。 如果不加Primary標簽啟動會報錯。
2 runtaskDatabaseIdProvider -> bean 配置是為了設置數據庫兼容的設置。該處設置的值可以在, mapper文件中取到,用于標記不同的數據庫,這一過程是有spring-mybatis 底層操作完成的。

數據源配置屬性文件
所在路徑
sping boot 配置多數據源

#數據庫配置
spring.datasource.driver-class-name=
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.jpa.show-sql=true

#定時任務數據源配置
#runtask.datasource.url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8
#runtask.datasource.username=root
#runtask.datasource.password=root
#runtask.datasource.driverClassName=com.mysql.jdbc.Driver
runtask.datasource.driverClassName=
runtask.datasource.url=
runtask.datasource.username=
runtask.datasource.password=
runtask.jpa.show-sql=true
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

西乌珠穆沁旗| 精河县| 太仓市| 那坡县| 友谊县| 乌拉特后旗| 崇礼县| 沙洋县| 洛隆县| 漯河市| 句容市| 忻城县| 葫芦岛市| 天长市| 瑞安市| 高陵县| 汤阴县| 西林县| 高尔夫| 泸州市| 贡觉县| 浠水县| 青海省| 沂水县| 惠东县| 镇沅| 南安市| 宝鸡市| 德昌县| 河曲县| 汾阳市| 扬州市| 成武县| 泸定县| 宣恩县| 蕉岭县| 驻马店市| 翁牛特旗| 玉龙| 神农架林区| 会宁县|