Spring配置多数据源

1. yml配置

注意:

  • yml配置库中,springboot2.0以上不要使用url,而是使用jdbc-url
  spring
  datasource:
      db1:
        jdbc-url: jdbc:oracle:thin:@10.132.119.102:1521:qmsdb
        username: mobile
        password: mobile
        driver-class-name: oracle.jdbc.OracleDriver
        hikari:
          idle-timeout: 30000
          connection-timeout: 15000
          connection-test-query: select 1 from dual
          login-timeout: 5
          max-lifetime: 900000
          maximum-pool-size: 20
          minimum-idle: 5
      db2:
        jdbc-url: jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 10.132.118.159)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = mesdbstb)))
        username: Kj
        password: kjmes
        driver-class-name: oracle.jdbc.OracleDriver
        hikari:
          idle-timeout: 30000
          connection-timeout: 15000
          connection-test-query: select 1 from dual
          login-timeout: 5
          max-lifetime: 900000
          maximum-pool-size: 20
          minimum-idle: 5

2. 配置类

注意:

  • 要把service层和dao层放到指定的数据源包下,当service只有实现类没接口时,可以配置一级目录
  • 当service有接口时,必须配置指向dao层接口路径
package cn.rp.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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 tk.mybatis.spring.annotation.MapperScan;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLClientInfoException;

/**
 * @version V1.0.0
 * @author: WangQingLong
 * @date: 2020/8/11 10:20
 * @description:
 *
**/
@Configuration
@MapperScan(basePackages = {"配置dao层接口路径"},sqlSessionFactoryRef = "sqlFact")
public class db1Config {


    /**
     * bean 里面的名字随便取
     * primary代表默认数据源
     * configuarationProperties配置的是yml里面配置的数据库前缀
     * 注意,下面方式只在spring当中有效
     * @return
     */
    @Bean(name = "dbOne")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public DataSource db1(){
        return DataSourceBuilder.create().build();
    }

    /**
     * 这里要注意的是,classpath:db1/*.xml是指resource资源文件夹下的db1的包下的xml文件
     * 也就是映射mapper.xml文件
     *
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean(name = "sqlFact")
    @Primary
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dbOne")DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
         //配置mybatis的驼峰映射
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        bean.setConfiguration(configuration);
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath: db1/*.xml"));
        return bean.getObject();
    }


    /**配置事务管理器
     * @param dataSource
     * @return
     */
    @Bean(name = "db1Tx")
    @Primary
    public DataSourceTransactionManager db1Tx(@Qualifier("dbOne")DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    /**模板
     * @param sqlSessionFactory
     * @return
     */
    @Bean(name = "db1Temp")
    @Primary
    public SqlSessionTemplate tb1Temp(@Qualifier("sqlFact")SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alan0517

感谢您的鼓励与支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值