资讯专栏INFORMATION COLUMN

spring-boot 同时配置Oracle和MySQL

zxhaaa / 699人阅读

摘要:同时配置和配置文件数据库驱动包因为在仓库下载不到,就直接下载手动导入配置文件数据源配置类测试类启动之后访问看后台有打印结果表示配置成功借鉴

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


    4.0.0

    com.adagio
    demo
    0.0.1-SNAPSHOT
    jar

    multiple-data-sources
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.8.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            mysql
            mysql-connector-java
            runtime
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



oralce数据库驱动包


    com.oracle
    ojdbc14
    10.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...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70628.html

相关文章

  • 分布式配置中心 duic

    摘要:什么是是配置管理中心,将配置统一管理提供标准的配置格式及编辑方式。如上图支持任何应用,任何语言的配置管理,,,等,同时采用语法作用配置文件格式,支持数据类型及结构化配置。前提创建数据库配置数据库连接将文件与文件放置在同一目录中。 什么是配置? 服务运行时能够通过外部动态修改的参数既是配置。在运行时动态变更服务的行为,避免业务发生变更需要修改代码或重启服务等等。 什么是 duic? du...

    justjavac 评论0 收藏0
  • 分布式配置中心 duic

    摘要:什么是是配置管理中心,将配置统一管理提供标准的配置格式及编辑方式。如上图支持任何应用,任何语言的配置管理,,,等,同时采用语法作用配置文件格式,支持数据类型及结构化配置。前提创建数据库配置数据库连接将文件与文件放置在同一目录中。 什么是配置? 服务运行时能够通过外部动态修改的参数既是配置。在运行时动态变更服务的行为,避免业务发生变更需要修改代码或重启服务等等。 什么是 duic? du...

    james 评论0 收藏0
  • 分布式配置中心 duic

    摘要:什么是是配置管理中心,将配置统一管理提供标准的配置格式及编辑方式。如上图支持任何应用,任何语言的配置管理,,,等,同时采用语法作用配置文件格式,支持数据类型及结构化配置。前提创建数据库配置数据库连接将文件与文件放置在同一目录中。 什么是配置? 服务运行时能够通过外部动态修改的参数既是配置。在运行时动态变更服务的行为,避免业务发生变更需要修改代码或重启服务等等。 什么是 duic? du...

    wangdai 评论0 收藏0

发表评论

0条评论

zxhaaa

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<