今天在做Springboot的小项目时,遇到了一个挺不容易发现的bug,给大家避避坑,在项目基本环境搭建好准备运行测试一下时,让人头疼的“小虫子”出现了:

"Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured."大致就是数据源中的"url"配置有问题,我刚开始数据源配置如下:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: root

我想可能是我"url"中有配置写错了,但是仔细检查并没有发现错误,然后我将"url"改为
url: jdbc:mysql://localhost:3306/reggie
但是还是会报相同的错误,我在网上查询,有人说因为导入了不需要的依赖导致,也有人说因为配置文件写错了,还有人说因为配置没有被扫描上导致等等,但是这些原因我一一排查了,还是报错,这就让人很头疼。
然后我仔细查看了Springboot数据源的配置文件示例,发现有示例在使用Druid数据源配置了
type: com.alibaba.druid.pool.DruidDataSource
而不是在"datasource"下直接加"druid",我去尝试了一下,发现用了"type"后正常运行了。

对于我刚开始的配置写法,我一直记得是这样写的,网上也有这样写法的示例,且IDEA也是这样提示的,所以我一直觉得自己配置没问题,耽误了很久,至于为什么这种写法会报错,个人猜想可能因为Springboot或者数据库版本更新。
也是给大家在遇到"Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured."时,提供一种思路吧。