SpringBoot项目启动问题(Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.)
SpringBoot项目启动报错:Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
异常现象
我在Spring Boot中关联MySQL、Mybatis进行数据库开发时,按照正常步骤添加了相关数据库的依赖,也进行了必要的数据库配置,结果在项目启动时出现如下异常信息:
看提示信息是说当前项目没有配置DataSource相关的配置!
解决方法
1、在application.yml/application.properties中添加数据库相关配置;
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf8
&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
&nullCatalogMeansCurrent=true
username: root
password: root
注意:在添加数据库配置文件时,要注意url的时区配置,有可能是因为没有配置时区导致报错;
设置时区时,添加serverTimezone
参数,比如serverTimezone=GMT%2B8
(URL 中 %2B表示+号,GMT%2B8即为GMT+8)。
GMT+8
表示东八区,北京时间Asia/Shanghai
表示上海时间,也是东八区
2、在@SpringBootApplication
注解中进行数据库配置的排除,即@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
。这也是最简单的解决办法。
修改之后我们来运行一下:
那么我们可以看到,项目也是成功启动了!不知道这篇文章对你有没有帮助呢,喜欢的话给作者点个赞吧!