1.在pom.xml里面添加依赖:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
2.如上ojdbc6在maven中央仓库里面是没有 的 所以需要手动注入到本地maven仓库中,首先下载这jar包到项目根目录下 如下:
3.在idea中Terminala输入maven注入命令:mvn install:install-file -Dfile=ojdbc6-11.2.0.3.jar -Dpackaging=jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3 如果报错先配置好maven环境变量 再在idea中Terminal中输入命令:SET JAVA_HOME=C:\Program Files\Java\jdk1.8.0_91 即可 如下:
4.在application.yml配置中配置多数据源地址 配置如下:
#当前使用配置配置
spring:
application:
datasource:
xh:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://192.168.0.123:3306/confluence_manage?characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=UTC&serverTimezone=Asia/Shanghai
username: root
password: 123456
wd:
driver-class-name: oracle.jdbc.OracleDriver
jdbc-url: jdbc:oracle:thin:@192.168.0.144:1521:orcl
username: root
password: 123456
5.多数据源配置 代码如下:
package com.longjin.comm.db;
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.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
* 多数据源的时候需要配置
*
*
*/
@Configuration
public class DataSourceConfig {
//系统数据库
@Bean(name = "wdDataSource")
@Qualifier("wdDataSource")
@ConfigurationProperties(prefix="spring.datasource.wd")
public DataSource wdDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "wdJdbcTemplate")
public JdbcTemplate wdJdbcTemplate(@Qualifier("wdDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
//
@Bean(name = "xhDataSource")
@Qualifier("xhDataSource")
@Primary
@ConfigurationProperties(prefix="spring.datasource.xh")
public DataSource xhDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "xhJdbcTemplate")
public JdbcTemplate xhJdbcTemplate(@Qualifier("xhDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
6.在dao层注入多数据源Bean 如下:
7.最后写相应的service层和controller层 在postman中调用接口 如下: