参考:MybatisPlus 配置多源数据库
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
spring:
datasource:
dynamic:
primary: master
strict: false
datasource:
master:
url: jdbc:mysql://127.0.0.1:3306/hikari-cloud?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
username: xxxxx
password: xxxxx
driver-class-name: com.mysql.jdbc.Driver
hikari:
max-lifetime: 60000
slave_1:
url: jdbc:mysql://127.0.0.1:3306/ms-test?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
username: xxxxx
password: xxxxx
driver-class-name: com.mysql.jdbc.Driver
hikari:
max-lifetime: 60000
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.hikari.cloud.data.entity
这样就配置完了,只需要在调用的mapper添加@DS(“数据库名”)进行指定数据库的查询,主数据库不需要配置
@Repository
@Mapper
@DS("slave_1")
public interface db2Mapper extends BaseMapper<db2> {
}
|