0315-Java学习-091-【答疑解惑】Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured

一、问题现象 

创建了一个 springboot 项目后,运行项目提示信息如下所示

 

相应的文本信息如下: 

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、未能配置数据源:“url”属性未指定,无法配置嵌入式数据源。

2、无法确定合适的驱动程序类别。

 

二、 问题解决:

  1、问题分析:

  在程序的配置文件中添加引入了数据库驱动依赖包,springboot 程序就自动去进行数据连接,但是因为没有配置数据库的连接信息,所以就会报错。

  2、问题解决:
  (1)删除数据库启动依赖:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.30</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.11</version>
</dependency>

 

  (2)去掉自动连接:

  在未配置数据库连接信息时,可以将入口类中的注解修改为以下代码:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

 

  即添加(exclude = {DataSourceAutoConfiguration.class}),排除自动连接。

 

  (3)配置数据源信息:

  在application.yml中配置数据库的连接信息,运行程序就不会报错。

 

posted @ 2023-01-24 21:15  范丰平  Views(125)  Comments(0Edit  收藏  举报