1. 程式人生 > >SpringBoot2 啟動報錯 Failed to auto-configure a DataSource

SpringBoot2 啟動報錯 Failed to auto-configure a DataSource

控制檯列印輸出

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

解決方法(一)

需要在啟動類@SpringBootApplication中新增exclude= {DataSourceAutoConfiguration.class},排除此類的autoconfig。啟動以後就可以正常執行。這是因為添加了資料庫元件,所以autoconfig會去讀取資料來源配置,而新建的專案還沒有配置資料來源,所以會導致異常出現。

解決方法(二)

在pom.xml檔案中去掉相應的JDBC的導包配置

        <!--<dependency>-->
            <!--<groupId>org.mybatis.spring.boot</groupId>-->
            <!--<artifactId>mybatis-spring-boot-starter</artifactId>-->
            <!--<version>1.3.2</version>-->
        <!--</dependency>-->

        <!--<dependency>-->
            <!--<groupId>mysql</groupId>-->
            <!--<artifactId>mysql-connector-java</artifactId>-->
            <!--<scope>runtime</scope>-->
        <!--</dependency>-->

           <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-jdbc</artifactId>-->
        <!--</dependency>-->

解決方法(三)

在application.yml中新增相應的資料庫配置變數