一、背景
在SpringBoot Mybatis 项目中,需要连接 多个数据源,连接多个数据库,需要连接一个MySQL数据库和一个Oracle数据库
二、依赖 pom.xml
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- MySQL --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.26</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><!-- Oracle --><dependency><groupId>com.oracle.database.jdbc</groupId><artifactId>ojdbc8</artifactId><version>19.8.0.0</version></dependency><!-- Redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>2.4.4</version></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.16</version><scope>provided</scope></dependency><dependency><groupId>javax.persistence</groupId><artifactId>javax.persistence-api</artifactId><version>2.2</version></dependency><!--swagger2--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><!--swagger-ui--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency><!-- https://mvnrepository.com/artifact/cn.easyproject/orai18n --><dependency><groupId>cn.easyproject</groupId><artifactId>orai18n</artifactId><version>12.1.0.2.0</version></dependency></dependencies>
三、项目结构
四、application.yml
spring.datasource.url数据库的JDBC URL
spring.datasource.jdbc-url用来重写自定义连接池
Hikari没有url属性,但是有jdbcUrl属性,在这中情况下必须使用jdbc_url
server:port: 8080spring:datasource:primary:jdbc-url: jdbc:mysql://localhost:3306/database_nameusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driversecondary:jdbc-url: jdbc:oracle:thin:@localhost:1521/ORCLusername: rootpassword: 123456driver-class-name: oracle.jdbc.driver.OracleDriver
五、MySQL配置类
MysqlDataSourceConfig
使用注解@Primary
配置默认数据源
package com.example.multipledata.config.mysqlconfig;import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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 = MysqlDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "mysqlSqlSessionFactory")
public class MysqlDataSourceConfig {static final String PACKAGE = "com.example.multipledata.mapper.mysqlmapper";static final String MAPPER_LOCATION = "classpath*:mapper/mysqlmapper/*.xml";@Primary@Bean(name = "mysqlDataSource")@ConfigurationProperties(prefix = "spring.datasource.primary")public DataSource mysqlDataSource() {return DataSourceBuilder.create().build();}@Primary@Bean(name = "mysqlTransactionManager")public DataSourceTransactionManager mysqlTransactionManager() {return new DataSourceTransactionManager((mysqlDataSource()));}@Primary@Bean(name = "mysqlSqlSessionFactory")public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource mysqlDatasource) throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(mysqlDatasource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MysqlDataSourceConfig.MAPPER_LOCATION));return sessionFactory.getObject();}
}
六、Oracle配置类
OracleDataSourceConfig
package com.example.multipledata.config.oracleconfig;import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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 = OracleDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "oracleSqlSessionFactory")
public class OracleDataSourceConfig {static final String PACKAGE = "com.example.multipledata.mapper.oraclemapper";static final String MAPPER_LOCATION = "classpath*:mapper/oraclemapper/*.xml";@Bean(name = "oracleDataSource")@ConfigurationProperties(prefix = "spring.datasource.secondary")public DataSource oracleDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "oracleTransactionManager")public DataSourceTransactionManager oracleTransactionManager() {return new DataSourceTransactionManager(oracleDataSource());}@Bean(name = "oracleSqlSessionFactory")public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource oracleDataSource) throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(oracleDataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(OracleDataSourceConfig.MAPPER_LOCATION));return sessionFactory.getObject();}
}
原文地址:
https://www.cnblogs.com/windy-xmwh/p/14748567.html