今天學習springboot,使用idea創建項目。在選擇組件時添加了mysq 然后在第一次啟動的時候啟動報錯,錯誤信息如下:
***************************
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).
在多方查證后,需要在啟動類的@EnableAutoConfiguration或@SpringBootApplication中添加exclude
= {DataSourceAutoConfiguration.class},排除此類的autoconfig。啟動以后就可以正常運行。
這是因為添加了數據庫組件,所以autoconfig會去讀取數據源配置,而我新建的項目還沒有配置數據源,所以會導致異常出現。
解決問題方法2:不用改動添加語句,注意項目文件結構的正確性