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

然后就可以测试使用啦~

相关推荐
公贵买其鹿8 分钟前
List深拷贝后,数据还是被串改
java
云和数据.ChenGuang1 小时前
Django 应用安装脚本 – 如何将应用添加到 INSTALLED_APPS 设置中 原创
数据库·django·sqlite
woshilys2 小时前
sql server 查询对象的修改时间
运维·数据库·sqlserver
Hacker_LaoYi2 小时前
SQL注入的那些面试题总结
数据库·sql
建投数据3 小时前
建投数据与腾讯云数据库TDSQL完成产品兼容性互认证
数据库·腾讯云
xlsw_3 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
Hacker_LaoYi4 小时前
【渗透技术总结】SQL手工注入总结
数据库·sql
岁月变迁呀4 小时前
Redis梳理
数据库·redis·缓存
独行soc4 小时前
#渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍06-基于子查询的SQL注入(Subquery-Based SQL Injection)
数据库·sql·安全·web安全·漏洞挖掘·hw
神仙别闹4 小时前
基于java的改良版超级玛丽小游戏
java