我的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]
这是正确的驱动器类路径吗?还是其他?
答案 0 :(得分:1)
尝试使用 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>