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


錯誤日志:

2019-07-13 16:04:26.318  INFO 21144 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-07-13 16:04:26.325  WARN 21144 --- [           main] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
 com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
2019-07-13 16:04:26.333  INFO 21144 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-07-13 16:04:26.339 ERROR 21144 --- [           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).

Disconnected from the target VM, address: '127.0.0.1:7870', transport: 'socket'

Process finished with exit code 1

問題分析及解決方案

問題原因: Mybatis沒有找到合適的加載類,其實是大部分spring - datasource - url沒有加載成功,分析原因如下所示.

  1. DataSourceAutoConfiguration會自動加載.

  2. 沒有配置spring - datasource - url 屬性.

  3. spring - datasource - url 配置的地址格式有問題.

  4. 配置 spring - datasource - url的文件沒有加載.

  5. profiles文件夾名稱沖突
方案一 (解決原因1)

排除此類的autoconfig。啟動以后就可以正常運行。

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})

但是這個顯然是治標不治本的,除非你不打算引用mybatis。

方案二 (解決原因2)

在application.properties/或者application.yml文件中沒有添加數據庫配置信息.

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/read_data?useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
方案三(解決原因3)
spring.datasource.url = jdbc:mysql://192.168.0.20:1504/f_me?setUnicode=true&characterEncoding=utf8
方案四 (解決原因4)

yml或者properties文件沒有被掃描到,需要在pom文件中<build></build>添加如下.來保證文件都能正常被掃描到並且加載成功.

<!-- 如果不添加此節點mybatis的mapper.xml文件都會被漏掉。 -->
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>
方案五 (解決原因5)

以上4個我逐一嘗試了,仍然沒能解決問題,於是想到會不會是profiles文件夾名稱問題,將profiles修改成config,問題解決。

如圖:

報錯時:

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

解決后:

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

特別感謝以下鏈接地址,以上部分信息直接借鑒的以下地址中的內容:

v源碼地址

https://github.com/toutouge/javademosecond/tree/master/hellolearn


作  者:請叫我頭頭哥
出  處:http://www.cnblogs.com/toutou/
關於作者:專注於基礎平台的項目開發。如有問題或建議,請多多賜教!
版權聲明:本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。
特此聲明:所有評論和私信都會在第一時間回復。也歡迎園子的大大們指正錯誤,共同進步。或者直接私信
聲援博主:如果您覺得文章對您有幫助,可以點擊文章右下角推薦一下。您的鼓勵是作者堅持原創和持續寫作的最大動力!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM