springBoot Oracle+SQLServer多數據源配置

第一步引入依賴

<!--sqlServer驅動依賴-->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
        </dependency>
 <!--數據庫連接配置,開始:-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>

第二步 yml文件配置

spring:
  datasource:
        jczb:
          username: xxx
          password: xxx
          url: jdbc:oracle:thin:@127.0.0.1:1521:orcl
          driver-class-name: oracle.jdbc.driver.OracleDriver
          druid:
            initial-size: 5

        weiBiao:
          url: jdbc:sqlserver://127.0.0.1:1433;databasename=CH_DATA_LOC
          driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
          username: xxx
          password: xxx

第三步 數據源配置

package com.xlt.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.autoconfigure.jdbc.DataSourceBuilder;
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 javax.sql.DataSource;


/**
 * @Classname DataSourceConfig1
 * @Description 主數據源配置
 * @Date 2019/11/27 10:00
 * @Created by xm
 */

@Configuration
// 配置主數據源mapper位置
@MapperScan(basePackages = "com.xlt.jczb.mapper", sqlSessionFactoryRef = "jczbSqlSessionFactory")
public class DataSourceConfigJczb {

    // 將這個對象放入Spring容器中
    @Bean(name = "jczbDataSource")


    // 表示這個數據源是默認數據源
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.jczb")
    public DataSource getDateSource() {
        return DataSourceBuilder.create().build();
    }



    @Bean(name = "jczbSqlSessionFactory")
    @Primary
    public SqlSessionFactory jczbSqlSessionFactory(@Qualifier("jczbDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 設置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath:/com/xlt/jczb/mapper/xml/*.xml"));
        return bean.getObject();
    }


    @Bean("jczbSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate jczbSqlSessionTemplate(
            @Qualifier("jczbSqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}
package com.xlt.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.autoconfigure.jdbc.DataSourceBuilder;
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 javax.sql.DataSource;

/**
 * @Classname DataSourceConfig2
 * @Description 第二數據源配置
 * @Date 2019/11/27 10:13
 * @Created by xm
 */
@Configuration
@MapperScan(basePackages = "com.xlt.weiBiao.mapper", sqlSessionFactoryRef = "weiBiaoSqlSessionFactory")
public class DataSourceConfigWeiBiao {


    @Bean(name = "weiBiaoDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.weiBiao")
    public DataSource getDateSource() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "weiBiaoSqlSessionFactory")
    public SqlSessionFactory weiBiaoSqlSessionFactory(@Qualifier("weiBiaoDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:/com/xlt/weiBiao/mapper/xml/*.xml"));
        return bean.getObject();
    }


    @Bean("weiBiaoSqlSessionTemplate")
    public SqlSessionTemplate weiBiaoSqlSessionTemplate(
            @Qualifier("weiBiaoSqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章