springboot连接mysql和sqlserver数据库配置两种数据源并实现动态切换

本文介绍了如何在SpringBoot应用中配置主数据源为MySQL,第二数据源为SQLServer,并实现数据源的动态切换。通过AOP切面和注解方式,实现了在运行时根据需求选择合适的数据库连接。
摘要由CSDN通过智能技术生成
在这里插入代码片
```#项目服务器配置
spring:
  datasource:
    master:
      jdbc-url: jdbc:mysql://localhost:****/db_**?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=true
      username: root
      password: ****
      initialSize: 1
      minIdle: 5
      maxActive: 100
      maxWait: 60000
      testWhileIdle: true
      testOnReturn: false
      testOnBorrow: false
      validationQuery: SELECT 1
      removeAbandoned: true
      removeAbandonedTimeout: 1800
      logAbandoned: true
      filters: mergeStat
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 25200000
    other:
      jdbc-url: jdbc:sqlserver://192.168.*.***;databasename=****
      username: **
      password: ******
      initialSize: 1
      minIdle: 5
      maxActive: 100
      maxWait: 60000
      testWhileIdle: true
      testOnReturn: false
      testOnBorrow: false
      validationQuery: SELECT 1s
      removeAbandoned: true
      removeAbandonedTimeout: 1800
      logAbandoned: true
      filters: mergeStat
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 25200000

      redis:
        host: 127.0.0.1
        password:
        jedis:
          pool:
            min-idle: 0
            max-idle: 20
            #            max-wait: 5000
            max-active: 100

主数据源配置


package com.**.admin.config;

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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.**.dao.mapper",sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MybatisDbMasterConfig {
    @Primary
    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource dataSource(){
        return DataSourceBuilder.create().build();
    }
    @Primary
    @Bean(name = "masterSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource)throws Exception{
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("com.****.dao.entity");
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/mj/dao/mapper/*Mapper.xml"));
        return factoryBean.getObject();
    }
    //配置事务管理
    @Primary
    @Bean(name = "masterTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean(name = "masterSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("masterSqlSessionFactory
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值