【SpringBoot已解决】Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded

springboot启动项目报错,应该是数据库连接的问题,导致无法启动。

错误消息如下:

***************************
APPLICATION FAILED TO START
***************************
 
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).

************************** 第一种,项目不需要连接数据库,启动报错 *******************************

解决方法如下:

只要在将@SpringBootApplication修改为@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})就可以启动的时候不需要连接数据库。

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})


************************** 第2种,需要连接数据库,启动报错 **************************************

解决方法如下:

第一种,yml配置示例如下

#在application.properties/或者application.yml文件中没有添加数据库配置信息.
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

第二种,properties文件示例如下

spring.datasource.url = jdbc:mysql://localhost:3306/test?setUnicode=true&characterEncoding=utf8


第三种,mysql的版本不同,示例如下

#mysql8以下的版本,请检查pom.xml文件种依赖的mysql jar包的版本
driver-class-name: com.mysql.jdbc.Driver
 
#mysql8以下的url写法
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
 
#mysql8的版本写法,多了个cj
driver-class-name: com.mysql.cj.jdbc.Driver
 
#同时mysql8的url也需要加入时区,参照如下
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&tinyInt1isBit=false

第四种,项目没有加载到yml或者properties文件,特别是自己的pom打包是jar的项目,请查看自己的pom.xml文件中的packaging

<packaging>jar</packaging>


如果pom中指定使用jar,系统不会自动读取到yml或者properties文件的,需要我们手动配置pom.xml。

<!--build放在</dependencies>标签的后面,主要加入的是resources标签 -->
<!--resources标签可以告诉系统启动的时候能够读取到这些后缀的文件 -->
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>lib</directory>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
        </resources>
    </build>


其他的请去看原作者的文章

彻底解决Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource_renkai721的博客-CSDN博客


 

猜你喜欢

转载自blog.csdn.net/weixin_60387745/article/details/129024525