某日新創建一個spring boot項目,添加完依賴以后運行項目開始報錯:
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).

原因1:沒有配置文件
這個報錯主要是因為數據庫沒有配置,比如一開始這個項目,添加完mysql依賴后直接啟動項目導致沒有讀取到mysql的相關配置,這個時候可以先注釋掉mysql的依賴,然后刷新maven依賴重新啟動項目。
如果配置了相關的文件或者想要使用配置文件,可以繼續往下看。
原因2:配置不正確
如果確實加入了mysql的相關配置,大概率是配置的格式不正確,比如下面這樣:

一共兩個錯誤,一是spring前面有空格,它的層級和上面的port一級了。
二是url后面的配置沒有空格,可以看出url這個沒有正常變色
(這種寫法很像python!)
修改錯誤后,改成下面這樣:

server: port: 8082 # spring配置 spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver
重新啟動項目后就正常了。
如果報下面的錯請參考