有些同学在使用SpringBoot搭建项目时,启动Application提示如下错误:
Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
具体如图:

1、错误的意思大概是:未能确定合适的驱动程序类导致配置数据源失败
解决办法1:
可能的原因如下:
在pom.xml中导入了类似如下mybatis依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
但是在核心配置文件application.properties或application.yml中没有配置数据库连接相关的属性,比如url、dirver、username、password等。
解决:
在application.properties中新增数据库连接配置:
#数据库连接配置 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456
或在application.yml中新增数据库连接配置:
#spring配置
spring:
#数据库连接配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8&serverTimezone=UTC
username: root
password: 123456
解决办法2:
如果你添加了有关库的依赖但是又不想配置库的连接,可以使用Application启动类上@SpringBootApplication注解中配置如下即可:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
解决办法3:
如果第一种方式你尝试了但是还不能解决,还有一个可能的原因就是你的resources目录并没有设置为资源目录导致的,典型的特征就是Resources目录没有小黄标,如下:

我们需要打开File->Poroject Structure->Modules->Sources,展开目录找到resources目录将其置为Resources,如下:

然后apply->save,效果如下:






