Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource

出现原因:学习Spring security过程中,整合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}),表示在启动的时候不需要连接数据库。

思路二、需要连接数据库,启动报错

  • 方案一:修改yml配置文件,添加如下配置
#原因:在application.properties/或者application.yml文件中没有添加数据库配置信息.
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/user_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
  • 方案二:修改properties文件
spring.datasource.url = jdbc:mysql://localhost:3306/user_db?setUnicode=true&characterEncoding=utf8
  • 方案三:mysql的版本影响
# mysql8版本写法中多了个cj
driver-class-name: com.mysql.cj.jdbc.Driver
 
# mysql8的url也需要加入时区,参照如下
url: jdbc:mysql://localhost:3306/user_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&tinyInt1isBit=false



#mysql8以下的版本,需要检查pom.xml文件中依赖的mysql jar包的版本
driver-class-name: com.mysql.jdbc.Driver
 
#mysql8以下的url写法
url: jdbc:mysql://localhost:3306/user_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false
  • 方案四:项目没有加载到ymlproperties文件。例如:pom打包的是jar项目时,检查pom.xml文件中的打包方式packaging
<packaging>jar</packaging>

如果pom指定使用jar打包方式,系统不会自动读取ymlproperties文件,需要手动进行配置

<!--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>
  • 方案五:若项目中使用了SpringCloud+Nacos,启动项目时需要在【Program arguments】中手动指定【–spring.profiles.active=test】,那么在resource文件夹下就必须要有bootstarp-test.yml或者application-test.yml文件,同时配置文件中nacos地址里面也必须配置对应的命名空间和对应服务名称的yml文件。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    原文链接:https://blog.csdn.net/renkai721/article/details/112257894
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值