32位数据源中没有mysql_Spring boot 启动提示数据源错误

在启动 Spring Boot 的项目的时候提示数据源未配置的错误。

09:52:08.333 [main] DEBUG o.s.b.d.LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:233)
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:174)

Spring 会提示你完整的导致启动错误的信息是:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


Process finished with exit code 1

错误分析

从上面的启动信息来看,已经说得非常清楚了,就是因为你配置了 Spring 的数据组件,但是你没有配置相应的数据源。

因为这个会导致你的启动失败。

解决办法

有下面的集中解决办法:

加入 H2 包

最简单的解决办法就是在依赖中添加 H2 的数据库,如果你使用 Spring Batch 的话,这个组件也是需要的,因为 Spring 会使用 H2 为数据源。

添加数据源配置

如果你已经添加了数据库驱动,例如你添加了 mysql 的数据库驱动。

那么你需要制定 Mysql 的数据库连接参数。

spring.datasource.url=jdbc:mysql://localhost:3306/myDb
spring.datasource.username=user1
spring.datasource.password=pass
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

在启动时候不载入数据源配置。

你可用在启动的时候不载入数据源配置。

可用在启动类上面,添加下面的注解。

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

c1cfc4de8c717ea79fa9a64fa8d5b6cd.png

你也可以在启动配置文件上面,添加下面的内容,这样能够保证你在启动的时候不载入数据源配置类。

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAuto
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值