spring boot3配置dynamic多数据源,报错url找不到,解决方案

我在编写springboot项目时尝试使用dynamic-datasource实现多数据库连接

运行项目时报错退出

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

报错如下

***************************
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

可能存在的问题

1.pom依赖导入情况

我当时遇到的就是这个问题

请注意,如果是springboot3项目,导入的包是下面这样

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
    <version>4.3.0</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.9</version>
</dependency>

如果是springboot2项目

<!-- 多数据源 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
</dependency>

<!-- druid 官方 starter -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
</dependency>

忘记自己springboot版本,可以在pom文件开头找(如下)

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
2.application.yml文件是否写对

按照下面这种格式写

server:
  port: 9090
#    数据库
spring:
  datasource:
    dynamic:
      primary: mysql
      strict: false
      datasource:
        mysql:
          url: jdbc:mysql://localhost:3306/ias_info?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8
          username: root
          password: 123
          driver-class-name: com.mysql.cj.jdbc.Driver
        mysql1:
          url: jdbc:mysql://cent:3306/alarm?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8
          username: root
          password: 123
          driver-class-name: com.mysql.cj.jdbc.Driver

关于其它问题

推荐几个大佬的博客,希望有帮助

spring boot配置dynamic多数据源,报错url找不到,解决方案_java dev profile里配置了url 找不到-CSDN博客

完美解决failed to configure a datasource: ‘url‘ attribute is not specified and no em_failed to configure a datasource: 'url' attribute -CSDN博客

【精·超详细】SpringBoot 配置多个数据源(连接多个数据库)_springboot连接多个数据源-CSDN博客

dynamic-datasource-spring-boot-starter 是一个用于在 Spring Boot 应用中实现多数据源的开源项目。它提供了简单易用的配置方式和强大的功能,让我们能够轻松地管理和切换多个数据源。 使用 dynamic-datasource-spring-boot-starter 实现多数据源的步骤如下: 1. 引入 dynamic-datasource-spring-boot-starter 依赖。在 Maven 或 Gradle 中添加对该项目的依赖,即可将其引入到项目中。 2. 配置多个数据源。在应用的配置文件中,添加多个数据源的配置信息。可以为每个数据源指定不同的 URL、用户名、密码等参数。 3. 配置数据源路由策略。使用 dynamic-datasource-spring-boot-starter 提供的数据源路由策略,将数据源动态地路由到对应的方法或服务上。 4. 使用注解标识数据源。在需要访问不同数据源的方法或服务上,使用相应的注解标识数据源。dynamic-datasource-spring-boot-starter 提供了一系列的注解,如 @DataSource、@Master、@Slave 等,用于标识不同的数据源。 5. 运行应用。运行应用后,dynamic-datasource-spring-boot-starter 会根据配置和注解的设置,将请求路由到正确的数据源上。 通过以上步骤,我们就能够轻松地实现多数据源的管理和切换。dynamic-datasource-spring-boot-starter 的灵活性和高性能使得在实际项目中使用多数据源变得非常简单和高效。同时,该项目还提供了丰富的监控和调试功能,方便我们对多数据源的运行情况进行监控和优化。总之,dynamic-datasource-spring-boot-starter 是一个强大的工具,为我们实现多数据源提供了便捷和可靠的解决方案
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值