SpringBoot配置多个数据源

1.application.yml配置

  datasource:
    #数据库连接池
    one:   #数据库1
      jdbc-url: jdbc:mysql://0.0.0.0:3306/abc?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&allowMultiQueries=true
      username: ***
      password: ***
      driver-class-name: com.mysql.jdbc.Driver
      type: com.zaxxer.hikari.HikariDataSource
    two:  #数据库2
      jdbc-url: jdbc:sqlserver://0.0.0.0:1433;Databasename=***;trustServerCertificate=true
      username: ***
      password: ***
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      type: com.zaxxer.hikari.HikariDataSource
    three:  #数据库3
      jdbc-url: jdbc:sqlserver://0.0.0.0:1433;Databasename=abd;trustServerCertificate=true
      username: ***
      password: ***
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      type: com.zaxxer.hikari.HikariDataSource

2.cnfig文件夹文件

SpringBoot配置多个数据源_第1张图片
默认数据源需添加@Primary,其他数据源不用

package com.ctsi.ipark.basic.config;

/**
 * @author ccy
 * @description
 * @date 20221125 11:26
 */

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.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.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {"com..."}, sqlSessionTemplateRef = "db1SqlSessionTemplate")
public class DataSourceOneConfig {
    @Bean(name = "db1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.one") //读取application.yml中的配置参数映射成为一个对象
    @Primary //默认数据源
    public DataSource db1DataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "db1SqlSessionFactory")
    @Primary
    public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "db1TransactionManager")
    @Primary
    public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "db1SqlSessionTemplate")
    @Primary
    public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}


推广一下我的ChatGPT4编程小助手,可以帮助解决各种编程的问题:
gnomic.cn/agentCenter/detail?schemeNo=AG2024010510582395VQQE&type=1

你可能感兴趣的:(spring,boot,后端,java)