Spring boot 中怎么连接多数据源

发布时间:2021-06-17 13:50:13 作者:Leah
来源:亿速云 阅读:257

今天就跟大家聊聊有关Spring boot 中怎么连接多数据源,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

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.这里需要注意几个地方:

看完上述内容,你们对Spring boot 中怎么连接多数据源有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. Spring boot 连接多数据源
  2. spring boot多数据源搭建及配置mysql,redis,mongodb详细步骤

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot

上一篇:如何将python包发布到PyPI和制作whl文件

下一篇:c程序怎么编写x的y次方

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》