springboot连接多数据库
-多个数据源-->添加mybatis-plus依赖为了避免冲突,可以删除mybatis。
引入依赖
<!--多个数据源-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
添加mybatis-plus依赖 为了避免冲突,可以删除mybatis
在application.properties文件中配置
spring.datasource.dynamic.primary=master
spring.datasource.dynamic.strict=false
spring.datasource.dynamic.datasource.master.url=jdbc:mysql://192.168.21.37:3306/mall?useUnicode=true&allowPublicKeyRetrieval=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true
spring.datasource.dynamic.datasource.master.username=root
spring.datasource.dynamic.datasource.master.password=root123
spring.datasource.dynamic.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.slave.url=jdbc:mysql://192.168.21.37:3306/210_bm_product?useUnicode=true&allowPublicKeyRetrieval=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true
spring.datasource.dynamic.datasource.slave.username=root
spring.datasource.dynamic.datasource.slave.password=root123
spring.datasource.dynamic.datasource.slave.driver-class-name=com.mysql.cj.jdbc.Driver
默认mall数据库,那个地方使用bm-product-service数据库,那个地方加入注解@DS("slave")
可能会出现错误,可以加上mybatis-plus日志
mybatis-plus.mapper-locations=classpath:mappers/*xml mybatis-plus.type-aliases-package=com.beiyou.entity mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
更多推荐
所有评论(0)