mybatis配置多数据源,加载不同数据库。

package com.kedacom.moserver.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.kedacom.moserver.utils.KdvDecryptUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;


/**
 * @author yanyibo
 * @date 2022/11/21
 * @description
 */
@Component
public class ConnectionConfiguration {

    private static final String DIALECT_GAUSS = "gauss";
    private static final String DIALECT_HIGHGO = "highgo";
    private static final String DIALECT_MYSQL = "mysql";
    /**
     * 数据库方言
     */
    @Value("${db.dialect}")
    private String dialect;

    /**
     * mysql连接参数
     */
    @Value("${mysql.datasource.driver-class-name}")
    private String mysqlDriver;
    @Value("${mysql.datasource.url}")
    private String mysqlUrl;
    @Value("${mysql.datasource.username}")
    private String mysqlUsername;
    @Value("${mysql.datasource.password}")
    private String mysqlPassword;

    /**
     * gauss连接参数
     */
    @Value("${gauss.datasource.driver-class-name}")
    private String gaussDriver;
    @Value("${gauss.datasource.url}")
    private String gaussUrl;
    @Value("${gauss.datasource.username}")
    private String gaussUsername;
    @Value("${gauss.datasource.password}")
    private String gaussPassword;

    /**
     * highgo连接参数
     */
    @Value("${highgo.datasource.driver-class-name}")
    private String highgoDriver;
    @Value("${highgo.datasource.url}")
    private String highgoUrl;
    @Value("${highgo.datasource.username}")
    private String highgoUsername;
    @Value("${highgo.datasource.password}")
    private String highgoPassword;

    @Bean
    public DataSource druidDataSource() throws Exception {
        DruidDataSource dataSource = new DruidDataSource();
        if (DIALECT_MYSQL.equals(dialect)) {
            dataSource.setDriverClassName(mysqlDriver);
            dataSource.setUrl(mysqlUrl);
            dataSource.setUsername(mysqlUsername);
            dataSource.setPassword(KdvDecryptUtils.kdvDecrypt(mysqlPassword));
            return dataSource;
        }
        if (DIALECT_GAUSS.equals(dialect)) {
            dataSource.setDriverClassName(gaussDriver);
            dataSource.setUrl(gaussUrl);
            dataSource.setUsername(gaussUsername);
            dataSource.setPassword(KdvDecryptUtils.kdvDecrypt(gaussPassword));
            return dataSource;
        }
        if (DIALECT_HIGHGO.equals(dialect)) {
            dataSource.setDriverClassName(highgoDriver);
            dataSource.setUrl(highgoUrl);
            dataSource.setUsername(highgoUsername);
            dataSource.setPassword(KdvDecryptUtils.kdvDecrypt(highgoPassword));
            return dataSource;
        }
        throw new Exception("数据库方言配置异常, db.dialect: " + dialect);
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        // 创建 SqlSessionFactoryBean 对象
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        // 设置数据源
        factoryBean.setDataSource(dataSource);
        // 设置 mapper 映射文件路径
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        String mapperPathStr = "classpath:mappers/*.xml";
        if (DIALECT_GAUSS.equals(dialect)) {
            mapperPathStr = mapperPathStr.replace("*.xml", DIALECT_GAUSS + "/*.xml");
        } else if (DIALECT_HIGHGO.equals(dialect)) {
            mapperPathStr = mapperPathStr.replace("*.xml", DIALECT_HIGHGO + "/*.xml");
        }

        factoryBean.setMapperLocations(resolver.getResources(mapperPathStr));
        // 设置 VFS
        factoryBean.setVfs(SpringBootVFS.class);
        // 返回 SqlSessionFactory 对象
        return factoryBean.getObject();
    }

}

数据源依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.5</version>
        </dependency>
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值