在啓動 Spring Boot 的項目的時候提示數據源未配置的錯誤。java
09:52:08.333 [main] DEBUG o.s.b.d.LoggingFailureAnalysisReporter - Application failed to start due to an exception org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:233) at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:174)
Spring 會提示你完整的致使啓動錯誤的信息是:mysql
*************************** 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
從上面的啓動信息來看,已經說得很是清楚了,就是由於你配置了 Spring 的數據組件,可是你沒有配置相應的數據源。spring
由於這個會致使你的啓動失敗。sql
有下面的集中解決辦法:數據庫
最簡單的解決辦法就是在依賴中添加 H2 的數據庫,若是你使用 Spring Batch 的話,這個組件也是須要的,由於 Spring 會使用 H2 爲數據源。ide
若是你已經添加了數據庫驅動,例如你添加了 mysql 的數據庫驅動。spring-boot
那麼你須要制定 Mysql 的數據庫鏈接參數。ui
spring.datasource.url=jdbc:mysql://localhost:3306/myDb spring.datasource.username=user1 spring.datasource.password=pass spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
你可用在啓動的時候不載入數據源配置。url
可用在啓動類上面,添加下面的註解。spa
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
你也能夠在啓動配置文件上面,添加下面的內容,這樣可以保證你在啓動的時候不載入數據源配置類。
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAuto