SpringBoot无法自动创建数据库,请求排查启动异常与配置问题
问题:SpringBoot无法加载MySQL驱动导致数据库自动创建失败
我刚开始学习SpringBoot,按照教授指导的步骤操作,但无法自动创建数据库。日志显示无法加载MySQL驱动类com.mysql.cj.jdbc.Driver,导致数据源创建失败,应用启动终止。我的目标是在MySQL Workbench中生成对应的数据表。
配置文件(application.properties)
spring.datasource.url = jdbc:mysql://localhost:3306/dbLastNameDemo spring.datasource.username = root spring.datasource.password = password spring.jpa.database-platform = org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate.ddl-auto = update spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect server.error.include.stacktrace=never
控制台启动日志
2023-10-04 21:24:10.672 INFO 24832 --- [ restartedMain] c.c.delapena.DelapenaApplication : Starting DelapenaApplication using Java 19.0.1 on Daesut with PID 24832 (C:\Users\Daesut\Downloads\delapena\delapena\target\classes started by Daesut in C:\Users\Daesut\Downloads\delapena\delapena) 2023-10-04 21:24:10.673 INFO 24832 --- [ restartedMain] c.c.delapena.DelapenaApplication : No active profile set, falling back to 1 default profile: "default" 2023-10-04 21:24:10.717 INFO 24832 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable 2023-10-04 21:24:10.717 INFO 24832 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG' 2023-10-04 21:24:11.134 INFO 24832 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2023-10-04 21:24:11.145 INFO 24832 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 4 ms. Found 0 JPA repository interfaces. 2023-10-04 21:24:11.702 INFO 24832 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2023-10-04 21:24:11.714 INFO 24832 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2023-10-04 21:24:11.714 INFO 24832 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.80] 2023-10-04 21:24:11.774 INFO 24832 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2023-10-04 21:24:11.774 INFO 24832 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1057 ms 2023-10-04 21:24:11.821 ERROR 24832 --- [ restartedMain] com.zaxxer.hikari.HikariConfig : Failed to load driver class com.mysql.cj.jdbc.Driver from HikariConfig class classloader jdk.internal.loader.ClassLoaders$AppClassLoader@c387f44 2023-10-04 21:24:11.823 WARN 24832 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.RuntimeException: Failed to load driver class com.mysql.cj.jdbc.Driver in either of HikariConfig class loader or Thread context classloader 2023-10-04 21:24:11.825 INFO 24832 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2023-10-04 21:24:11.835 INFO 24832 --- [ restartedMain] ConditionEvaluationReportLoggingListener :
解决方案
添加MySQL驱动依赖:项目缺少MySQL JDBC驱动包,这是核心问题。
- Maven项目:在
pom.xml中添加以下依赖<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> - Gradle项目:在
build.gradle中添加runtimeOnly 'com.mysql:mysql-connector-j'
注:Spring Boot 2.4及以上版本推荐使用
mysql-connector-j,旧版本用mysql-connector-java。- Maven项目:在
提前创建数据库:Spring Boot只能自动生成数据表,无法自动创建数据库。需先在MySQL Workbench中执行SQL:
CREATE DATABASE dbLastNameDemo;可选:显式指定驱动类:虽然Spring Boot会自动推断,但可在application.properties中添加配置确保正确性:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver检查MySQL服务状态:确认本地MySQL服务正在运行,端口3306未被占用。
同步依赖:添加依赖后,执行Maven的
clean install或Gradle的build命令,确保依赖下载完成。
内容的提问来源于stack exchange,提问作者Daniel Gilbert Dela Peña
相关产品推荐
相关产品推荐

