无法确定合适的驱动程序类

9

启动应用程序时,出现以下错误:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blockDataController': Injection of resource dependencies failed;
    nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blockSummaryImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'prodCodeMapper' defined in file [D:\YueNiuProject\StockMarket\yueniu-stock-data\market-data-dao\target\classes\com\yueniu\stock\market\data\mapper\block\ProdCodeMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory';
    nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' 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 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

1
请分享您的application.properties和pom文件。 - kj007
当你运行应用程序时,确保所使用的数据库的JDBC驱动程序已包含在类路径中,并确保数据库连接URL是正确的。 - Jesper
发送代码或日志时,请使用代码格式 - Oleg Ushakov
3个回答

13

要连接到 SQL 数据库,您必须在 application.properties 中配置数据源。

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://..
spring.datasource.username=//..
spring.datasource.password=//..

1
如果您不需要配置数据源,可以使用exclude。示例代码如下:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

如果不使用默认配置,它将无法注册数据源,然后会抛出异常。


目前你的回答不够清晰,请编辑并添加更多细节,以帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Oleg Ushakov
如果你也有一个@EnableAutoConfiguration注解,你可能需要在那里指定 exclude,因为它似乎会覆盖 @SpringBootApplication - gidds

0
有时候,如果您将bean配置放在同一个包中,它可能无法正常工作。例如,加载属性的bean需要放在单独的包中。不确定这个答案是否会被接受,但对我来说,在将下面的代码移动到不同的包之后,它可以正常工作。
    @Bean
public PlatformTransactionManager oracleTransactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(oracleEntityManager().getObject());
    return transactionManager;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接