主要錯誤:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
未能配置數據源:未指定“url”屬性,也無法配置嵌入式數據源。
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-06-03 09:47:45.300 ERROR 23160 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
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).
原因:
spring boot默認會加載org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration類,DataSourceAutoConfiguration類使用了@Configuration注解向spring注入了dataSource bean。因為工程中沒有關於dataSource相關的配置信息,當spring創建dataSource bean因缺少相關的信息就會報錯。
兩種解決辦法
1.在Application類上加
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
2.配置數據源:
例如在application配置文件中:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://ip:端口號/數據庫名稱?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8