Springboot + MySQL + Driver Class Not Found
Springboot + MySQL + Driver Class Not Found
我正在进行一个简单的Spring Boot项目,其中包含一个线程池和MySQL,以便在添加spring-boot-starter-jdbc时,每当我连接到MySQL时,我都会遇到以下错误。
org.springframework.beans.factory.BeanCreationException: 在类路径资源[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]中定义的名为'dataSource'的bean创建失败:通过工厂方法实例化失败;内嵌异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.apache.tomcat.jdbc.pool.DataSource]:工厂方法'dataSource'抛出异常;内嵌异常是java.lang.IllegalStateException:无法加载驱动程序类:com.mysql.jdbc.Driver at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.s
更新1:
org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-web mysql mysql-connector-java runtime com.opencsv opencsv 3.3
在使用Spring Boot连接MySQL数据库时,可能会遇到"Driver Class Not Found"的错误。这个问题的出现原因是缺少MySQL驱动程序依赖。解决方法是在项目的pom文件中添加mysql-connector-java的依赖,并执行clean install命令来安装maven项目。如果使用的是Eclipse,还需要检查JDK版本和maven配置,有时候可能会将JRE误用为JDK,导致在控制台上看起来一切正常,但实际上未下载相应的jar文件。在编译时不会出现错误,是因为使用了JdbcTemplate,但在运行时会出现"Driver Class Not Found"的错误。
以下是解决该问题的一些有用链接:
- [stackoverflow.com/a/36079859/5086633](https://stackoverflow.com/a/36079859/5086633)
- [stackoverflow.com/questions/28821521](https://stackoverflow.com/questions/28821521)
- [stackoverflow.com/questions/28042426](https://stackoverflow.com/questions/28042426)