我的Spring Boot应用程序中有以下课程:
@Configuration
public class JDBCTokenConfig {
...
@Value("${spring.datasource.driver-class-name}")
private String dbDriverClassName;
@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(dbDriverClassName);
dataSource.setUrl(datasourceUrl);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
return dataSource;
}
我的pom.xml中也包含以下内容:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
在我的application.properties中,我具有:
...
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
但我看到以下错误:
Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.cj.jdbc.Driver]
这是正确的驱动器类路径吗?还是其他?
尝试使用com.mysql.jdbc.Driver
当pom.xml中未明确提供版本时,Spring使用以下预配置的版本进行依赖:https://docs.spring.io/platform/docs/current/reference/htmlsingle/#appendix-dependency-versions
这里将使用以下版本的mysql-connector-java。 5.1.47
对于mysql-connector-java v 5.1.47正确的驱动程序类是com.mysql.jdbc.Driver
您还可以提供版本依赖关系以使用最新的驱动程序:com.mysql.cj.jdbc.Driver
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>