SpringBoot整合MySQL启动失败:无法配置DataSource的url属性
解决SpringBoot集成MySQL启动失败问题
报错信息
*************************** 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
已排查项
- application.properties中指定的数据库已存在
- MySQL服务器正在运行
- pom.xml中已引入
mysql-connector-j依赖 - Maven依赖中已包含
mysql-connector-j-8.031.jar
环境版本
- java version "17.0.6" 2023-01-17 LTS
- Apache Maven 3.6.3
- Eclipse Version: 2022-12 (4.26.0)
相关配置
src/main/resources/application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/database_name spring.datasource.username= spring.datasource.password= spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect spring.jpa.generate-ddl=true spring.jpa.hibernate.ddl-auto=create spring.jpa.show-sql=true logging.level.org.hibernate.type.descriptor.sql=TRACE logging.level.org.hibernate.SQL=DEBUG logging.level.org.springframework=DEBUG
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!-- The basics --> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.7</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>MyProjectApplication</artifactId> <version>1.0.0</version> <!-- Dependencies --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> </dependencies> <!-- Build configuration --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
MyProjectApplication.java
package com.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication() public class MyProjectApplication { public static void main(String[] args) { SpringApplication.run(MyProjectApplication.class, "--debug"); } }
项目结构
src/ ├── main/ │ ├── java/ │ │ └── com/ │ │ └── example/ │ │ └── myproject/ │ │ ├── MyProjectApplication.java │ │ └── controllers/ │ │ └── MyController.java │ │ └── dao/ │ ... │ ... │ ├── resources/ │ │ ├── application.properties target/ pom.xml
解决方案
1. 补全数据库账号密码
application.properties中spring.datasource.username和spring.datasource.password为空,必须填入MySQL的有效账号密码,示例:
spring.datasource.username=root spring.datasource.password=your_mysql_password
2. 修正主类包扫描范围
主类MyProjectApplication.java所在包为com.springboot,但业务类均在com.example.myproject下。@SpringBootApplication默认仅扫描主类所在包及其子包,导致数据源配置无法被正确识别。两种解决方式:
- 将
MyProjectApplication.java移动到com.example.myproject包下 - 在
@SpringBootApplication中显式指定扫描范围:@SpringBootApplication(scanBasePackages = "com.example.myproject")
3. 调整MySQL驱动依赖作用域
为mysql-connector-j添加runtime作用域,符合SpringBoot依赖规范,确保运行时能正确加载驱动:
<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency>
4. 匹配MySQL 8.0对应的Hibernate方言
将spring.jpa.database-platform修改为适配MySQL 8.0的方言:
spring.jpa.database-platform = org.hibernate.dialect.MySQL8Dialect
5. 清理Eclipse缓存并重载Maven
- 执行
Project -> Clean清理项目 - 右键项目选择
Maven -> Update Project,勾选Force Update of Snapshots/Releases后点击OK
6. 验证配置文件位置
确认application.properties位于src/main/resources目录下,文件名无拼写错误(如不要写成application.properites)。
内容的提问来源于stack exchange,提问作者alienko
相关产品推荐
相关产品推荐

