登录
推荐 文章 Go 技术 课程 下载 专题 AI
首页 >  数据库 >  MySQL

spring-boot 同时配置Oracle和MySQL

来源:SegmentFault

时间:2023-01-18 07:56:21 463浏览 收藏

积累知识,胜过积蓄金银!毕竟在##column_title##开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《spring-boot 同时配置Oracle和MySQL》,就带大家讲解一下MySQL、Java、Oracle、springboot知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

Spring Boot 1.5.8.RELEASE同时配置Oracle和MySQL

配置POM文件

4.0.0com.adagiodemo0.0.1-SNAPSHOTjarmultiple-data-sourcesDemo project for Spring Bootorg.springframework.bootspring-boot-starter-parent1.5.8.RELEASEUTF-8UTF-81.8org.springframework.bootspring-boot-starter-jdbcorg.springframework.bootspring-boot-starter-webmysqlmysql-connector-javaruntimeorg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-maven-plugin
  • oralce数据库驱动包

com.oracleojdbc1410.2.0.4.0
  • 因为在maven仓库下载不到,就直接下载lib手动导入

配置文件

spring.datasource.primary.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=oracle.jdbc.OracleDriver

spring.datasource.secondary.url=jdbc:mysql://localhost:3306/bootdo
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver

数据源配置类

package com.adagio.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class DataSourceConfig {

    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @Primary
    @ConfigurationProperties(prefix="spring.datasource.secondary")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
    
    @Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(
            @Qualifier("primaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "secondaryJdbcTemplate")
    public JdbcTemplate secondaryJdbcTemplate(
            @Qualifier("secondaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }


}

测试类

package com.adagio.web;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CxzdbController {
    @Autowired
    @Qualifier("primaryJdbcTemplate")
    protected JdbcTemplate jdbcTemplate1;

    @Autowired
    @Qualifier("secondaryJdbcTemplate")
    protected JdbcTemplate jdbcTemplate2;
    
//    @Autowired
//    protected JdbcTemplate jdbcTemplate;


    @RequestMapping("/test")
    public List> getCxzdb(){
//        String sql = "SELECT * FROM sys_user";
        
        String sql = "SELECT * FROM USER";
        
        List> resObj = (List>) jdbcTemplate1.execute(new PreparedStatementCreator() {
            
            @Override
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                return con.prepareStatement(sql);
            }
        }, new PreparedStatementCallback>>() {

            @Override
            public List> doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                ps.execute();   
                ResultSet rs = ps.getResultSet();   
                while(rs.next()){
                    System.out.println("==" + rs.getString(1));
                    System.out.println("==" + rs.getString(2));
                    System.out.println("==" + rs.getString(3));
                    System.out.println("==" + rs.getString(4));
                    System.out.println("==" + rs.getString(5));
                    
//                    Map map = new HashMap();
//                    map.put("id", rs.getString("id"));
                    
                }
                return null;
            }
            
        } );
        return resObj;
    }
}
  • 启动之后访问:http://localhost:8080/test
  • 看后台有打印结果表示配置成功

借鉴:https://gitee.com/didispace/S...

今天关于《spring-boot 同时配置Oracle和MySQL》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于mysql的内容请关注golang学习网公众号!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表