關於使用spring.datasource.hikari連接數據庫找不到找不到url的問題

applica.properties裏配置連接數據庫使用hikari

#鏈接數據庫
spring.datasource.primary.jdbc-url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8 &serverTimezone=Asia/Shanghai
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver

出現的問題異常

Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.

未能配置數據源:未指定“url”屬性,也無法配置嵌入式數據源。

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-09-08 18:28:06.345 ERROR 7148 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


Process finished with exit code 1

原因:springboot連接數據庫是默認的是 :spring.datasource爲前綴

解決 配置重定義識別數據庫連接方式爲spring.datasource.primary

/**
 * 使用spring.datasource.hikari爲前綴連接數據庫
 * DataSource 默認爲spring.datasource連接
 */
@Configuration
public class Dateresourse {

    @Bean(name = "PrimaryDataSource") //作爲一個bean對象並命名
    @ConfigurationProperties(prefix = "spring.datasource.primary") //配置文件中,該數據源的前綴
    public DataSource PrimaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

完美解決

在這裏插入圖片描述

發佈了15 篇原創文章 · 獲贊 4 · 訪問量 2152
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章