成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

spring-boot 同時配置Oracle和MySQL

zxhaaa / 931人閱讀

摘要:同時配置和配置文件數(shù)據(jù)庫驅(qū)動包因為在倉庫下載不到,就直接下載手動導(dǎo)入配置文件數(shù)據(jù)源配置類測試類啟動之后訪問看后臺有打印結(jié)果表示配置成功借鑒

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數(shù)據(jù)庫驅(qū)動包


    com.oracle
    ojdbc14
    10.2.0.4.0

因為在maven倉庫下載不到,就直接下載lib手動導(dǎo)入

配置文件
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
數(shù)據(jù)源配置類
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

看后臺有打印結(jié)果表示配置成功

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

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/70628.html

相關(guān)文章

  • 分布式配置中心 duic

    摘要:什么是是配置管理中心,將配置統(tǒng)一管理提供標(biāo)準(zhǔn)的配置格式及編輯方式。如上圖支持任何應(yīng)用,任何語言的配置管理,,,等,同時采用語法作用配置文件格式,支持?jǐn)?shù)據(jù)類型及結(jié)構(gòu)化配置。前提創(chuàng)建數(shù)據(jù)庫配置數(shù)據(jù)庫連接將文件與文件放置在同一目錄中。 什么是配置? 服務(wù)運行時能夠通過外部動態(tài)修改的參數(shù)既是配置。在運行時動態(tài)變更服務(wù)的行為,避免業(yè)務(wù)發(fā)生變更需要修改代碼或重啟服務(wù)等等。 什么是 duic? du...

    justjavac 評論0 收藏0
  • 分布式配置中心 duic

    摘要:什么是是配置管理中心,將配置統(tǒng)一管理提供標(biāo)準(zhǔn)的配置格式及編輯方式。如上圖支持任何應(yīng)用,任何語言的配置管理,,,等,同時采用語法作用配置文件格式,支持?jǐn)?shù)據(jù)類型及結(jié)構(gòu)化配置。前提創(chuàng)建數(shù)據(jù)庫配置數(shù)據(jù)庫連接將文件與文件放置在同一目錄中。 什么是配置? 服務(wù)運行時能夠通過外部動態(tài)修改的參數(shù)既是配置。在運行時動態(tài)變更服務(wù)的行為,避免業(yè)務(wù)發(fā)生變更需要修改代碼或重啟服務(wù)等等。 什么是 duic? du...

    james 評論0 收藏0
  • 分布式配置中心 duic

    摘要:什么是是配置管理中心,將配置統(tǒng)一管理提供標(biāo)準(zhǔn)的配置格式及編輯方式。如上圖支持任何應(yīng)用,任何語言的配置管理,,,等,同時采用語法作用配置文件格式,支持?jǐn)?shù)據(jù)類型及結(jié)構(gòu)化配置。前提創(chuàng)建數(shù)據(jù)庫配置數(shù)據(jù)庫連接將文件與文件放置在同一目錄中。 什么是配置? 服務(wù)運行時能夠通過外部動態(tài)修改的參數(shù)既是配置。在運行時動態(tài)變更服務(wù)的行為,避免業(yè)務(wù)發(fā)生變更需要修改代碼或重啟服務(wù)等等。 什么是 duic? du...

    wangdai 評論0 收藏0

發(fā)表評論

0條評論

zxhaaa

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<