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。啓動以後就可以正常運行。