SpringCloud 项目启动出现未能配置数据源
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.查询自己的项目是否需要连接数据源 若不需要
启动了添加注解
@SpringBootApplication(,exclude = { DataSourceAutoConfiguration.class })
排除数据源的自动配置类。
2.、项目使用了Druid, 这时而又非原生的配置,比如增加了动态数据源支持。
这时需要在启动类上增加:
@SpringBootApplication(exclude ={DruidDataSourceAutoConfigure.class} )
排除掉Druid的数据源的自动配置类。
3.在 pom.xml 配置文件中查看是否配置了数据连接技术 spring-boot-starter-jdbc 包 ,在启动配置文件时 ,Spring Boot 的自动装配机制就会去配置文件中找,相关的数据库的连接配置信息,如果找不到则抛出异常信息
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
不需要就先注释掉
4.本地启动或者开发环境启动 可以选择先添加上 数据源的文件配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&serverTimezone=CTT
username: root
password: root
hikari:
原因:项目启动时引用了数据源配置的starter,但是配置文件又未按照starter里的配置方式配置,这时就会出现未能找到连接数据源。解决方式就是要么按照原生配置方式来配置,要么排除掉这个自动配置类。