SpringBoot+Druid测试连接不上的问题,报错 ‘url’ attribute is not specified and no embedded datas

文章讲述了在SpringBoot应用中使用Druid作为数据源时遇到的错误,发现配置问题导致未找到合适的驱动。通过移除Druid依赖并添加type属性指定DruidDataSource,解决了启动时的错误,提示读者注意配置文件的正确设置。
摘要由CSDN通过智能技术生成

0 环境

操作系统:windows10家庭中文版

集成开发环境:idea2022.03

框架:SpringBoot 3.2.3

Druid: 1.2.19版本

1 问题

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-03-12T18:48:50.196+08:00 ERROR 10128 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 
 

***************************
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).

 报错信息显示,没有找到合适的数据源驱动。

但是博主的pom和properties配置文件没有发现问题:

pom.xml:

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>3.0.3</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-3-starter</artifactId>
			<version>1.2.19</version>
		</dependency>
		<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter-test</artifactId>
			<version>3.0.3</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfigure</artifactId>
			<version>3.2.3</version>
		</dependency>
	</dependencies>

properties:

spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.druid.username=root
spring.datasource.druid.password=123456

2 分析

将properties文件中的druid去掉:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=123456

结果:

报错消失,但是使用的并不是druid连接池,而是使用的是SpringBoot自带的Hikari连接池。说明问题没有解决,但是可以确定是properties配置的问题,springboot启动的时候没有加载到druid驱动。

3 解决方案

在properties配置文件中加入一行:

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

即,完整的properties配置文件如下:

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=123456

运行:

问题解决!!!

PS:如果是yml文件,则可以参考如下文章!!!

4 参考

springboot使用druid报错 ‘url’ attribute is not specified and no embedded datasource could be configured.

  • 16
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值