springboot整合多数据源

一.依赖导入(boot版本2.1.7)

       
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/>
    </parent>
    <properties>
        <resteasy.version>3.0.5.Final</resteasy.version>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <mybatis-plus.version>3.2.0</mybatis-plus.version>
        <swagger.ui>2.9.2</swagger.ui>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

        <!-- 动态数据源 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>2.5.6</version>
        </dependency>
 
       <!--tomcat datasource-->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
        </dependency>

        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>

 

二.配置文件

spring:
  application:
    name: xx_xx
  profiles:
    include: test
  datasource:
    dynamic:
      # 是否开启 SQL日志输出,生产环境建议关闭,有性能损耗
      p6spy: false
      hikari:
        connection-timeout: 30000
        max-lifetime: 1800000
        max-pool-size: 15
        min-idle: 5
        connection-test-query: select 1
      # 配置默认数据源
      primary: mysql
      datasource:
        # 数据源-1,名称为 primary
        mysql:
          username: xx
          password: xx
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://localhost:3306/xx?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8&autoReconnect=true&failOverReadOnly=false
          initialSize: 5
          minIdle: 5
          maxActive: 20
          maxWait: 60000
          timeBetweenEvictionRunsMillis: 60000
          minEvictableIdleTimeMillis: 30000
          validationQuery: SELECT 1 FROM DUAL
          testWhileIdle: true
          testOnBorrow: true
          testOnReturn: true
        mysql2:
          username: xx
          password: xx
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://xx:3306/xx?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8&autoReconnect=true&failOverReadOnly=false
          initialSize: 5
          minIdle: 5
          maxActive: 20
          maxWait: 60000
          timeBetweenEvictionRunsMillis: 60000
          minEvictableIdleTimeMillis: 30000
          validationQuery: SELECT 1 FROM DUAL
          testWhileIdle: true
          testOnBorrow: true
          testOnReturn: true
#        #数据源-2,名称为 oracle
        oracle:
          username: xx
          password: xx
          driver-class-name: oracle.jdbc.driver.OracleDriver
          url: jdbc:oracle:thin:@xx:1521:xx
          validationQuery: SELECT 1 FROM DUAL
          initialSize: 5
          minIdle: 5
          maxActive: 20
          maxWait: 60000
          timeBetweenEvictionRunsMillis: 60000
          minEvictableIdleTimeMillis: 30000
          testWhileIdle: true
          testOnBorrow: true
          testOnReturn: true

 

三.数据源配置类编写

1.mysql配置类编写


import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
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.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 org.springframework.transaction.PlatformTransactionManager;

@Configuration
/**指定dao的位置*/
@MapperScan(basePackages = "com.xx.dao.mysql" ,sqlSessionTemplateRef = "mysqlSqlSessionTemplate")
public class MysqlSource {

    //创建数据源
    @Bean(name="mysqlDataSource")
    /**指定数据源的前缀*/
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.mysql")
    @Primary
    public DataSource dataSource() {
        return new DataSource();
    }

    /**
     * 创建SqlSessionFactory
     */
    @Bean(name = "mysqlSqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        sqlSessionFactoryBean.setDataSource(dataSource());

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        //指定mapper文件所在目录
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/mysql/*Mapper.xml"));
        //此处加入多维mybatis拦截器
        Interceptor[] interceptorArry = new Interceptor[1];
        interceptorArry[0] = new AutoMapperInterceptor();
        sqlSessionFactoryBean.setPlugins(interceptorArry);

        return sqlSessionFactoryBean.getObject();
    }

    /**创建事务管理器*/
    @Bean(name = "mysqlTransactionManager")
    @Primary
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }


    @Bean(name = "mysqlSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);

    }

}

2.oracle配置类编写



import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;


@Configuration
/**指定dao的位置*/
@MapperScan(basePackages = "com.xx.dao.oracle" ,sqlSessionTemplateRef = "oracleSqlSessionTemplate")
public class OracleSource {

    //创建数据源
    @Bean(name="oracleDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.oracle")//指定数据源的前缀

    public DataSource dataSource() {
        return new DataSource();
    }

    //创建SqlSessionFactory
    @Bean(name = "oracleSqlSessionFactory")

    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        sqlSessionFactoryBean.setDataSource(dataSource());

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        //指定mapper文件所在目录
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/oracle/*Mapper.xml"));

        return sqlSessionFactoryBean.getObject();
    }

    //创建事务管理器
    @Bean(name = "oracleTransactionManager")

    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }


    @Bean(name = "oracleSqlSessionTemplate")
    public SqlSessionTemplate oracleSqlSessionTemplate(@Qualifier("oracleSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);

    }
}

3.dao包位置

4.mapper文件位置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值