springboot+mysql+oracle+mybatis多数据源配置

最近项目中需要整合各系统,需要在一个项目中访问mysql,两个oracle的数据源,记录一下。

1.pom文件配置


        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.0
        

        
        
            mysql
            mysql-connector-java
        
        
        
            com.oracle
            ojdbc6
            11.2.0.3
        

2.yml文件配置

spring:
  datasource:
    acdb:
      jdbc-url: jdbc:mysql://192.168.0.*:3306/*?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
      username: ####
      password: ####
      driver-class-name: com.mysql.cj.jdbc.Driver
#    配置erp数据源
    erpdb:
      jdbc-url: jdbc:oracle:thin:@192.168.0.*:1521:*
      username: ####
      password: ####
      driver-class-name: oracle.jdbc.driver.OracleDriver
#      配置OA数据源
    oadb:
      jdbc-url: jdbc:oracle:thin:@192.168.0.*:1521:*
      username: ####
      password: ####
      driver-class-name: oracle.jdbc.driver.OracleDriver

3.项目结构

springboot+mysql+oracle+mybatis多数据源配置_第1张图片

此处需要注意的是,在oa,erp目录下的service需要再加一层目录,我这里不加目录出现了扫描不到文件的情况。

4.三个配置类

第一个ac的配置类,此处需要注意的是@primary标签,默认的数据源,其他两个配置类不需要添加这个标签;

package com.sword.storageac.common.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.sword.storageac.ac.*.mapper", sqlSessionTemplateRef = "acSqlSessionTemplate")
public class AcDataSourceConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.acdb")
    @Primary
    public DataSource acDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public SqlSessionFactory acSqlSessionFactory(@Qualifier("acDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/ac/*Mapper.xml"));
        return bean.getObject();
    }

    @Bean
    @Primary
    public DataSourceTransactionManager acTransactionManager(@Qualifier("acDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    @Primary
    public SqlSessionTemplate acSqlSessionTemplate(@Qualifier("acSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

第二个erp的配置类

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

import javax.sql.DataSource;


@Configuration
@MapperScan(basePackages = "com.sword.storageac.erp.*.mapper", sqlSessionTemplateRef = "erpSqlSessionTemplate")
public class ErpDataSourceConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.erpdb")
    public DataSource erpDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public SqlSessionFactory erpSqlSessionFactory(@Qualifier("erpDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/erp/*Mapper.xml"));
        return bean.getObject();
    }

    @Bean
    public DataSourceTransactionManager erpTransactionManager(@Qualifier("erpDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public SqlSessionTemplate erpSqlSessionTemplate(@Qualifier("erpSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

第三个为oa的配置类

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

import javax.sql.DataSource;


@Configuration
@MapperScan(basePackages = "com.sword.storageac.oa.*.mapper", sqlSessionTemplateRef = "oaSqlSessionTemplate")
public class OaDataSourceConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.oadb")
    public DataSource oaDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public SqlSessionFactory oaSqlSessionFactory(@Qualifier("oaDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/oa/*Mapper.xml"));
        return bean.getObject();
    }

    @Bean
    public DataSourceTransactionManager oaTransactionManager(@Qualifier("oaDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public SqlSessionTemplate oaSqlSessionTemplate(@Qualifier("oaSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

OK,大功告成,三个数据源已配置完成。需要注意的是,配置文件中配置扫描路径的话,是一配置文件为主,不读取yml的配置的扫描路径。

你可能感兴趣的:(springboot)