springboot整合oracle并实现多数据源
前文:工作之余忽然想写一个springboot+oracle+vue的项目,想把工作老掉牙的spring+struts+jsp项目给换掉!老项目维护起来真难熬啊!新项目开始的时间遇到了多数据源的问题,网上找了很多多少代码案例,但是都没有解决问题!于是总结了一个demo,方便大家学习使用!
1.项目准备
1.项目创建
我这里是直接用的maven项目,然后自己导入的依靠!
https://i-blog.csdnimg.cn/blog_migrate/a95431cb8cbb1a72284b4398e98544e5.png
2.导入依靠
项目结构如下:
https://i-blog.csdnimg.cn/blog_migrate/86a038bf69f1883eb93a3135b81be606.png
导入依靠:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.17</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!--Web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<!--jdbc依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--oracle依赖-->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
</dependency>
</dependencies>
</project> 3.添加配置文件
新建一个application.properties
# database1
spring.datasource.primary.url=jdbc:oracle:thin:@localhost:1521:orcl20
spring.datasource.primary.username=test1
spring.datasource.primary.password=test1
spring.datasource.primary.driver-class-name=oracle.jdbc.driver.OracleDriver
# database2
spring.datasource.secondary.url=jdbc:oracle:thin:@localhost:1521:orcl3
spring.datasource.secondary.username=test2
spring.datasource.secondary.password=test2
spring.datasource.secondary.driver-class-name=oracle.jdbc.driver.OracleDriver https://i-blog.csdnimg.cn/blog_migrate/30e8272e87efb7a153b215ab21aad371.png
4.启动类创建
创建com.xyl.Application.java文件
package com.xyl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
https://i-blog.csdnimg.cn/blog_migrate/753446385d609d67f2305188f25e9b5c.png
5.数据源配置类
创建com.xyl.config.DataSourceConfig.java文件
package com.xyl.config;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean(name = "primaryDataSource")
public DataSource primaryDataSource(@Value("${spring.datasource.primary.url}") String url,
@Value("${spring.datasource.primary.username}") String username,
@Value("${spring.datasource.primary.password}") String password,
@Value("${spring.datasource.primary.driver-class-name}") String driverClassName) {
HikariConfig config = new HikariConfig();
config.setJdbcUrl(url);
config.setUsername(username);
config.setPassword(password);
config.setDriverClassName(driverClassName);
return new HikariDataSource(config);
}
@Bean(name = "secondaryDataSource")
public DataSource secondaryDataSource(@Value("${spring.datasource.secondary.url}") String url,
@Value("${spring.datasource.secondary.username}") String username,
@Value("${spring.datasource.secondary.password}") String password,
@Value("${spring.datasource.secondary.driver-class-name}") String driverClassName) {
HikariConfig config = new HikariConfig();
config.setJdbcUrl(url);
config.setUsername(username);
config.setPassword(password);
config.setDriverClassName(driverClassName);
return new HikariDataSource(config);
}
@Bean(name = "oneTemplate")
public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "twoTemplate")
public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
6.dao和controller创建
这里为了测试,只创建dao和controller类,接口也没去写。大家工作和学习中万万别学我这样啊!
com.xyl.dao.TestDao.java
com.xyl.controller.TestController.java
package com.xyl.dao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Slf4j
@Repository
public class TestDao {
@Resource
private JdbcTemplate oneTemplate;
@Resource
private JdbcTemplate twoTemplate;
public List<Map<String,Object>> getTest(){
String sql = " select * from AAA where ROWNUM <= 10";
List<Map<String, Object>> tableData = oneTemplate.queryForList(sql);
log.info("这是在database1中获取的数据:{}",tableData);
sql = " select * from AAwhere ROWNUM <= 10";
return twoTemplate.queryForList(sql);
}
}
https://i-blog.csdnimg.cn/blog_migrate/3fe7c60b80e898ebbc9f80258d1fe7af.png
package com.xyl.controller;
import com.xyl.dao.TestDao;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@RequestMapping("/test")
@RestController
public class TestController {
@Resource
private TestDao testDao;
@RequestMapping("/get")
public List<Map<String,Object>> getTest(){
return testDao.getTest();
}
}
https://i-blog.csdnimg.cn/blog_migrate/005bf65cf202ed2d5191c4012430565e.png7.启动项目看运行结果
https://i-blog.csdnimg.cn/blog_migrate/660e590efd77cdfb869f8e25fd55a7a3.png
https://i-blog.csdnimg.cn/blog_migrate/46b324f77cb488dac396c414109af004.png 抱歉哈,database2里面AA表忘记添加数据啦!
总结:demo到这里就已经结束啦。盼望可能帮到各位!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]