SpringBoot 2.0 啓動報錯 Failed to auto-configure a DataSource

springboot 2.0 啓動報錯 Failed to auto-configure a DataSource

原因

在pom中添加了mybatis,mysql依賴後,如果application.properties中沒有配置數據庫連接方面的配置,則會報錯:

這是因爲添加了數據庫組件,所以autoconfig會去讀取數據源配置,而我新建的項目還沒有配置數據源,所以會導致異常出現。
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-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).

解決方法

1.在啓動類中加註解

在啓動類的 @EnableAutoConfiguration 或 @SpringBootApplication 中添加
exclude = {DataSourceAutoConfiguration.class},排除此類的autoconfig。啓動以後就可以正常運行。

2.在配置文件application.properties中添加數據源配置,或在pon.xml去除maven依賴

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章