SpringBoot 2.0 報錯: Failed to configure a DataSource: 'url' attribute is not specified and no embe


問題描述

***************************
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).

問題分析及解決方案

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

  1. DataSourceAutoConfiguration會自動加載.

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

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

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

 

網上給出了這幾種解決方案.

方案一 (解決原因1)

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

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})

  

方案二 (解決原因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>

  

說明:

小編工作中試過方案四,完全可行


免責聲明!

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



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