1.问题描述

        在使用 springboot3.0+ 集成 mybatis + 多数据源过程中遇到的问题,如果在网上搜索了方法无法解决,不妨修改下配置依赖

Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.String

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

 也不需要排除 @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

2.依赖 

各位注意一下依赖吧

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.10</version>
    <relativePath/>
</parent>

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
    <version>4.2.0</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
    <version>3.5.5</version>
</dependency>

3.配置文件

spring: 
  datasource:
    dynamic:
      primary: master
      strict: false
      datasource:
        master:
          url: jdbc:postgresql://{host:port}/{db}?allowMultiQueries=true
          username: username
          password: pwd
          driver-class-name: org.postgresql.Driver
        slave:
          url: jdbc:postgresql://{host:port}/{db}?allowMultiQueries=true
          username: username
          password: pwd
          driver-class-name: org.postgresql.Driver

4.mapper配置

       mapper上添加对应的数据源即可

@Mapper
@DS(value = "master")
public interface UserMapper {
    User findById(@Param("userId") String userId);
}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐