springBoot Oracle+SQLServer多数据源配置

第一步引入依赖


        
            com.microsoft.sqlserver
            sqljdbc4
            4.0
        
 
        
            com.alibaba
            druid
            1.1.6
        
        
            com.oracle
            ojdbc6
            11.2.0.3
        

第二步 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);
    }
}

 

你可能感兴趣的:(后端)