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);
}