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);
    }
}

然后就可以测试使用啦~

相关推荐
魔道不误砍柴功4 分钟前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python
NiNg_1_2345 分钟前
SpringBoot整合SpringSecurity实现密码加密解密、登录认证退出功能
java·spring boot·后端
闲晨7 分钟前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
种树人202408198 分钟前
如何在 Spring Boot 中启用定时任务
spring boot
测开小菜鸟2 小时前
使用python向钉钉群聊发送消息
java·python·钉钉
Ai 编码助手3 小时前
MySQL中distinct与group by之间的性能进行比较
数据库·mysql
P.H. Infinity3 小时前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天3 小时前
java的threadlocal为何内存泄漏
java
陈燚_重生之又为程序员3 小时前
基于梧桐数据库的实时数据分析解决方案
数据库·数据挖掘·数据分析
caridle3 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express