前文:
工作之余忽然想写一个springboot+oracle+vue的项目,想把工作老掉牙的spring+struts+jsp项目给换掉!老项目维护起来真难熬啊!新项目开始的时间遇到了多数据源的问题,网上找了很多多少代码案例,但是都没有解决问题!于是总结了一个demo,方便大家学习使用!
1.项目准备
1.项目创建
我这里是直接用的maven项目,然后自己导入的依靠!
2.导入依靠
项目结构如下:
导入依靠:
- <?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
复制代码
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);
- }
- }
复制代码
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 AA where ROWNUM <= 10";
- return twoTemplate.queryForList(sql);
- }
- }
复制代码
- 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();
- }
- }
复制代码 7.启动项目看运行结果
抱歉哈,database2里面AA表忘记添加数据啦!
总结:demo到这里就已经结束啦。盼望可能帮到各位!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |