springboot+mybatisplus 配置多数据源
1、pom.xml文件引入dynamic-datasource-spring-boot-starter包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
2、application.properties配置多个数据源
spring.datasource.dynamic.datasource.master.url=jdbc:sqlserver://xxx.xx.xx.xxx:1433;DatabaseName=db
spring.datasource.dynamic.datasource.master.username=sa
spring.datasource.dynamic.datasource.master.password=sa
spring.datasource.dynamic.datasource.master.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.dynamic.datasource.slave_1.url=jdbc:sqlserver://xxx.xx.xx.xxx:1433;DatabaseName=db1
spring.datasource.dynamic.datasource.slave_1.username=sa
spring.datasource.dynamic.datasource.slave_1.password= sa
spring.datasource.dynamic.datasource.slave_1.driver-class-name= com.microsoft.sqlserver.jdbc.SQLServerDriver
3.在dao层引入 (在service层、方法上添加也可) @DS("slave_1")
@Mapper
@DS("slave_1")
public interface AreaMapper extends BaseMapper<AreaEntity> {
}
引入之后该dao、service、方法就会匹配到对应数据源进行查询 默认是master
本文地址:https://blog.csdn.net/c410425783/article/details/110950010
上一篇: sqlserver如何删除表中除主键外一模一样的数据
下一篇: java 集合类的使用心得