springboot之多數據源配置JdbcTemplate

来源:https://www.cnblogs.com/haizhilangzi/archive/2018/10/11/9770362.html
-Advertisement-
Play Games

springboot多數據源配置,代碼如下 配置文件 application.properties 測試代碼如下 在運行的時候會出現如下異常問題,運行失敗,報出java.lang.IllegalArgumentException: jdbcUrl is required with driverCla ...


springboot多數據源配置,代碼如下

DataSourceConfig
package com.rookie.bigdata.config;

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

import javax.sql.DataSource;

/**
 * @author
 * @date 2018/10/10
 */
@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);
    }

}

 

StudentServiceImpl
package com.rookie.bigdata.service;

import com.rookie.bigdata.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * @author
 * @date 2018/10/9
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    @Qualifier("primaryJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Autowired
    @Qualifier("secondaryJdbcTemplate")
    private JdbcTemplate jdbcTemplate2;

    /**
     * 採用第一個暑假源進行插入數據
     * @param student
     */
    @Override
    public void create(Student student) {

        jdbcTemplate.update("INSERT INTO student(stu_no,name,age)VALUE (?,?,?)", student.getStuNo(), student.getName(), student.getAge());

    }

    /**
     * 第一個數據源進行插入數據
     * @param stuNo
     */
    @Override
    public void deleteByNo(Integer stuNo) {
        jdbcTemplate.update("DELETE  FROM  student WHERE stu_no=?", stuNo);
    }

    /**
     * 第二個數據源進行查詢數據
     * @param stuNo
     * @return
     */
    @Override
    public Integer queryByStuNo(Integer stuNo) {
        return jdbcTemplate2.queryForObject("select count(1) from student", Integer.class);
    }
}

 

配置文件 application.properties

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

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

 

測試代碼如下

package com.rookie.bigdata.service;

import com.rookie.bigdata.domain.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author liuxili
 * @date 2018/10/10
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentServiceImplTest {

    @Autowired
    private StudentServiceImpl studentService;

    @Test
    public void create1() throws Exception {
        Student student = new Student();
        student.setStuNo(1L);
        student.setName("張三");
        student.setAge(23);

        studentService.create(student);

    }

    @Test
    public void deleteByName1() throws Exception {
        studentService.deleteByNo(1);
    }

    @Test
    public void queryByStuNo1() throws Exception {
        System.out.println(studentService.queryByStuNo(1));

    }


}

 

在運行的時候會出現如下異常問題,運行失敗,報出java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.異常

後來經過查資料,發現出現該問題的原因是由於springboot版本的問題,本實例採用的springboot版本為2.0.5,如果將版本改為1.5以前的版本就不會出現如上的問題,其實解決上面的異常主要有如下兩種解決方案

方案一:

 按照如下方案進行修改application.properties配置文件,如下:修改完成後,上面的異常不會再出現

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

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

 

方案二:

  原有的application.properties配置文件不進行修改,修改DataSourceConfig類中的信息,如下:修改完成後,異常也不會再出現能夠正常運行

  application.properties

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

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

 

  DataSourceConfig

  

package com.rookie.bigdata.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
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;

import javax.sql.DataSource;

/**
 * @author
 * @date 2018/10/10
 */
@Configuration
public class DataSourceConfig {

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

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

    @Primary
    @Bean(name = "primaryDataSourceProperties")
    @Qualifier("primaryDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSourceProperties primaryDataSourceProperties() {
        return new DataSourceProperties();
    }


    @Bean(name = "secondaryDataSourceProperties")
    @Qualifier("secondaryDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSourceProperties secondaryDataSourceProperties() {
        return new DataSourceProperties();
    }




    @Primary
    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource primaryDataSource() {

        return primaryDataSourceProperties().initializeDataSourceBuilder().build();
    }


    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource secondaryDataSource() {

        return primaryDataSourceProperties().initializeDataSourceBuilder().build();
    }


}

 

至此,springboot 採用多數據源對JdbcTemplate進行配置完美解決

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 商品,黃金交易流程最基礎、最核心的環節,無商品不電商。商品數據無處不在,商家(採銷、供應商)發佈管理、供應商下採購單、倉儲配送、促銷、搜索、商詳頁展現、購物支付、財務結算、售後服務等,都離不開商品。商品信息要準確傳導於京東整個供應鏈的各節點,必須要有一套穩健、可靠的商品服務體系支撐。 原本並沒有統一 ...
  • 在數據分析當中的東西還是很多的,我在這裡只是啟髮式的介紹一下,瞭解到這方面的東西之後,使用的時候可以更快的找到解決辦法,希望能對大家有所幫助。 這次,依然是使用的sklearn中的iris數據集,對其進行通過熱圖來展示。 預處理 sklearn.preprocessing是機器學習庫中預處理的模塊, ...
  • 一、前言 Thymeleaf 的出現是為了取代 JSP,雖然 JSP 存在了很長時間,併在 Java Web 開發中無處不在,但是它也存在一些缺陷: 1、JSP 最明顯的問題在於它看起來像HTML或XML,但它其實上並不是。大多數的JSP模板都是採用HTML的形式,但是又摻雜上了各種JSP標簽庫的標 ...
  • 在賬戶表的基礎上,我新建了一個賬戶account_session表,用來記錄登錄賬戶的account_id和最新一次登錄成功用戶的session_id,然後首先要修改登錄方法:每次登錄成功後,要將登錄用戶信息寫入Session的同時還要更新account_session表裡相應賬戶的session_ ...
  • 本次和大家分享的主要是docker搭建es和springboot操作es的內容,也便於工作中或將來使用方便,因此從搭建es環境開始到代碼插入信息到es中;主要節點如下: 1.elasticsearch啟動 我本機環境是windows10,要掛載es的配置文件需要在本機上創建配置文件,因此這裡創建配置 ...
  • 直接gdb pgname 參數1 這種方式,參數1是不會帶到gdb里的 1,首先啟動程式 2,設置程式的參數 ...
  • 在上文 設計一個百萬級的消息推送系統 中提到消息流轉採用的是 Kafka 作為中間件。 其中有朋友咨詢在大量消息的情況下 Kakfa 是如何保證消息的高效及一致性呢? ...
  • spring-boot-starter-web:spring-boot-starter:spring-boot場景啟動器;幫我們導入了web模塊 正常運行所依賴的組件; Spring Boot將所有的功能場景都抽取出來,做成一個個的starters(啟動器), 只需要在項目裡面引入這些starter ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...