SpringBoot多数据源配置及使用

1.application.properties数据配置

首先现在配置文件中定义三个数据库相关信息

# 数据库1
targetLibraryMain.datasource.url = jdbc:kingbase8://127.0.0.1:54321/DATA_ONE?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&allowMultiQueries=true
targetLibraryMain.datasource.driver-class-name = com.kingbase8.Driver
targetLibraryMain.datasource.username = DATA_ONE
targetLibraryMain.datasource.password = 1234567

# 数据库2
resourceCenterMain.datasource.url = jdbc:kingbase8://127.0.0.1:54321/DATA_TWO?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&allowMultiQueries=true
resourceCenterMain.datasource.driver-class-name = com.kingbase8.Driver
resourceCenterMain.datasource.username = DATA_TWO
resourceCenterMain.datasource.password = 12345678

# 数据库3
superviseBusinessMain.datasource.url = jdbc:kingbase8://127.0.0.1:54321/DATA_THREE?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&allowMultiQueries=true
superviseBusinessMain.datasource.driver-class-name = com.kingbase8.Driver
superviseBusinessMain.datasource.username = DATA_THREE
superviseBusinessMain.datasource.password = 123456789

2.创建数据库配置类

java 复制代码
package xx.config.dataSourceCfg;

import xx.utils.DataSrcUtil;
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.beans.factory.annotation.Value;
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 javax.sql.DataSource;

@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = TargetLibraryMainDataSourceCfg.PACKAGE, sqlSessionFactoryRef = "targetLibraryMainSqlSessionFactory")
public class TargetLibraryMainDataSourceCfg {
    static final String PACKAGE = "xx.mapper.targetLibraryMain";
    static final String MAPPER_LOCATION = "classpath:xx/mapper/targetLibraryMain/*.xml";

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

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

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

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


    @Bean(name = "targetLibraryMainDataSource")
    @Primary
    public DataSource targetLibraryMainDataSource() {
        return DataSrcUtil.getDataSource(driverClass, url, user, password);
    }

    @Bean(name = "targetLibraryMainTransactionManager")
    @Primary
    public DataSourceTransactionManager targetLibraryMainTransactionManager() {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(this.targetLibraryMainDataSource());
        return transactionManager;
    }

    @Bean(name = "targetLibraryMainSqlSessionFactory")
    @Primary
    public SqlSessionFactory targetLibraryMainSqlSessionFactory(@Qualifier("targetLibraryMainDataSource") DataSource targetLibraryMainDataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(targetLibraryMainDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(TargetLibraryMainDataSourceCfg.MAPPER_LOCATION));
        // 实体类文件存放地址
        sessionFactory.setTypeAliasesPackage("xx.pojo");
        return sessionFactory.getObject();
    }
}

参考以上文件再创建两个用来对应其他两个数据库信息

3.Application主启动类配置

java 复制代码
// 手动配置数据源(exclude = {DataSourceAutoConfiguration.class})
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

然后就可以测试使用啦~

相关推荐
九圣残炎24 分钟前
【从零开始的LeetCode-算法】3354. 使数组元素等于零
java·算法·leetcode
王ASC43 分钟前
ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值。ojdbc8版本23.2.0.0驱动BUG【已解决】
数据库·sql·oracle
Dlwyz1 小时前
问题: redis-高并发场景下如何保证缓存数据与数据库的最终一致性
数据库·redis·缓存
天天扭码1 小时前
五天SpringCloud计划——DAY1之mybatis-plus的使用
java·spring cloud·mybatis
程序猿小柒1 小时前
leetcode hot100【LeetCode 4.寻找两个正序数组的中位数】java实现
java·算法·leetcode
如意机反光镜裸2 小时前
如何快速将Excel数据导入到SQL Server数据库
数据库
不爱学习的YY酱2 小时前
【操作系统不挂科】<CPU调度(13)>选择题(带答案与解析)
java·linux·前端·算法·操作系统
丁总学Java2 小时前
Maven项目打包,com.sun.tools.javac.processing
java·maven
不爱学习的啊Biao2 小时前
初识mysql数据库
数据库·mysql·oracle
kikyo哎哟喂2 小时前
Java 代理模式详解
java·开发语言·代理模式