springboot项目禁用dataSource数据源功能,只需修改yml文件,关闭数据库连接功能

当我们遇到以下报错:

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).

 

如果不想使用数据库功能,可以这样配置:

application.yml: 

spring:
  autoconfigure:
    exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot项目中配置多个数据源可以通过以下步骤实现: 1. 禁用Spring Boot数据源自动配置类。在@SpringBootApplication注解中添加exclude属性,值为DataSourceAutoConfiguration.class,如下所示: ```java @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class DatasourceDomeApplication { public static void main(String[] args) { SpringApplication.run(DatasourceDomeApplication.class, args); } } ``` 2. 创建自定义的数据源配置类。在该类中使用@Configuration注解,并定义多个@Bean方法,每个方法返回一个DataSource对象,用于初始化不同的数据源。可以使用@ConfigurationProperties(prefix = "spring.datasource.mysql-datasourceX")注解来指定每个数据源的属性配置,如下所示: ```java @Configuration public class DataSourceConfig { @Bean(name = "mysqlDataSource1") @ConfigurationProperties(prefix = "spring.datasource.mysql-datasource1") public DataSource dataSource1(){ DruidDataSource build = DruidDataSourceBuilder.create().build(); return build; } @Bean(name = "mysqlDataSource2") @ConfigurationProperties(prefix = "spring.datasource.mysql-datasource2") public DataSource dataSource2(){ DruidDataSource build = DruidDataSourceBuilder.create().build(); return build; } } ``` 3. 在application.yml配置文件中配置数据源相关的属性。可以根据要配置每个数据源的连接信息、用户名、密码等,如下所示: ```yaml spring: datasource: mysql-datasource1: url: jdbc:mysql://localhost:3306/db1 username: root password: password1 mysql-datasource2: url: jdbc:mysql://localhost:3306/db2 username: root password: password2 ``` 通过以上步骤,你可以成功配置多个数据源,并在项目中使用对应的数据源进行数据库操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Spring Boot配置多数据源的四种方式](https://blog.csdn.net/qq_45515182/article/details/126330084)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [SpringBoot数据源配置](https://blog.csdn.net/u012060033/article/details/123759694)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ThatMonth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值