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

溫馨提示×

溫馨提示×

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

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

Spring boot 連接多數據源

發布時間:2020-07-22 19:42:06 來源:網絡 閱讀:912 作者:龍愛海 欄目:編程語言

1.application.yml中添加兩個datasource

server:
  port: 8080
spring:
  application:
    name: king
  datasource:
    master:
      type: com.zaxxer.hikari.HikariDataSource
      jdbc-url: jdbc:mysql://localhost:3306/king?useUnicode=yes&characterEncoding=UTF-8&serverTimezone=UTC
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: root
      password: 123456
      idle-timeout: 10000
      minimum-idle: 1
      maximum-pool-size: 10
      pool-name: master
      auto-commit: false
      connection-test-query: SELECT 1
    slave:
      type: com.zaxxer.hikari.HikariDataSource
      jdbc-url: jdbc:mysql://localhost:3306/king?useUnicode=yes&characterEncoding=UTF-8&serverTimezone=UTC
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: root
      password: 123456
      idle-timeout: 10000
      minimum-idle: 1
      maximum-pool-size: 10
      pool-name: slave
      auto-commit: false
      connection-test-query: SELECT 1
2.手動初始化數據源masterDataSource
package cn.bj.king.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
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 org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
* @author ARongking
* @date 2019-03-28
*/
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"cn.bj.king.mapper.master"},
        sqlSessionFactoryRef = "masterSqlSessionFactory")
public class WriteDataSourceConfig {

    @Bean(name = "masterDataSource")
    @Primary
    @Qualifier("masterDataSource")
    public DataSource masterDataSource(@Qualifier("masterHikariConfig")HikariConfig hikariConfig) {
        System.out.println("實例化主庫");
        HikariDataSource hikariDataSource=new HikariDataSource(hikariConfig);
        return hikariDataSource;
    }

    /**
    * 配置連接池信息
    * @return
    */
    @ConfigurationProperties(prefix = "spring.datasource.master")
    @Bean("masterHikariConfig")
    public HikariConfig masterHikariConfig(){
        HikariConfig hikariConfig=new HikariConfig();
        return hikariConfig;
    }

    /**
     * SqlSessionFactory配置
     *
     * @return
     * @throws Exception
     */
    @Bean(name = "masterSqlSessionFactory")
    @Primary
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        // 配置mapper文件位置
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/master/*.xml"));
        sqlSessionFactoryBean.setTypeAliasesPackage("cn.bj.king.entity");
        return sqlSessionFactoryBean.getObject();
    }

    /**
     * 配置事物管理器
     *
     * @return
     */
    @Bean(name = "masterTransactionManager")
    @Primary
    public DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }
}
3.手動初始化數據源ReadDataSource
package cn.bj.king.config;

import com.github.pagehelper.PageInterceptor;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
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.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"cn.bj.king.mapper.slave"},
        sqlSessionFactoryRef = "slaveSqlSessionFactory")
public class ReadDataSourceConfig {

    @Autowired
    Environment environment;

    @Bean(name = "slaveDataSource")
    @Qualifier("slaveDataSource")
    public DataSource slaveDataSource(@Qualifier("slaveHikariConfig")HikariConfig hikariConfig) {
        System.out.println("實例化從庫");
        HikariDataSource hikariDataSource=new HikariDataSource(hikariConfig);
        return hikariDataSource;
    }

    /**
     * 配置連接池信息
     * @return
     */
    @ConfigurationProperties(prefix = "spring.datasource.slave")
    @Bean("slaveHikariConfig")
    public HikariConfig slaveHikariConfig(){
        HikariConfig hikariConfig=new HikariConfig();
        return hikariConfig;
    }

    /**
     * SqlSessionFactory配置
     *
     * @return
     * @throws Exception
     */
    @Bean(name = "slaveSqlSessionFactory")
    public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        // 配置mapper文件位置
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/slave/*.xml"));
        sqlSessionFactoryBean.setTypeAliasesPackage("cn.bj.king.entity");
        return sqlSessionFactoryBean.getObject();
    }

    /**
     * 配置事物管理器
     *
     * @return
     */
    @Bean(name = "slaveTransactionManager")
    public DataSourceTransactionManager slaveTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }
}
4.測試啟動是否正常初始化master和slave 兩個數據源
5.這里需要注意幾個地方:
    * 配置數據庫連接池的時候,如果不是手動設置HikariConfig ,即通過Environment 去一個個獲取數據庫連接池在yml中的配置的話,數據庫連接池的(idle-timeout,minimum-idle, maximum-pool-size,pool-name,auto-commit,connection-test-query)等,一定要與slave節點平級,不然 HikariConfig 是讀取不到這些信息的。
    * 如果手動賦值HikariConfig 的話,那就沒有什么限制了,寫到哪里都行,只需要用Environment  去讀取配置文件的值,手動設置到HikariConfig 就行了。
向AI問一下細節

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

AI

北碚区| 周口市| 鹤山市| 泰顺县| 金华市| 福泉市| 大新县| 灵璧县| 辛集市| 山阳县| 中宁县| 吉首市| 顺平县| 丰顺县| 太保市| 萍乡市| 剑川县| 阜宁县| 沈丘县| 西吉县| 和平区| 东宁县| 罗定市| 普定县| 敦煌市| 称多县| 班戈县| 剑河县| 开阳县| 永丰县| 前郭尔| 小金县| 岳阳县| 土默特左旗| 宁南县| 武平县| 武威市| 广宁县| 竹北市| 偃师市| 瑞丽市|