方法1: 使用自带的 多数据源
方法2: 使用 aspectj aop
关键类 : AbstractRoutingDataSource, 在当前线程中绑定一个数据源 既 ThreadLocal<DataSource> 变量
public class DynamicDataSource extends AbstractRoutingDataSource { /** * 重写此方法, * @return DataSource */ @Override protected Object determineCurrentLookupKey() { return DataSourceType.getDataBaseType(); } }
参考:
spring mybatis 多数据源 https://www.cnblogs.com/chen-msg/p/7485701.html
spring 多数据源 两种方式 https://blog.csdn.net/tuesdayma/article/details/81081666
# 一个配置文件
spring: application: name: cjy-api-test # 数据源 datasource: # 数据源 1 datacenter: username: root password: xxxx type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.NonRegisteringDriver jdbcUrl: jdbc:mysql://xxx:3306/xxxx?useSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8 minimum-idle: 1 maximum-pool-size: 2 # 数据源 2 monitor: username: root password: xxxx type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.NonRegisteringDriver jdbcUrl: jdbc:mysql://xxx:3306/xxxx?useSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8 minimum-idle: 1 maximum-pool-size: 2 mvc: view: suffix: .jsp prefix: / # 日志的根目录 log: home: ${log:E:/logs/spring-cloud/xxxxxx} # mybatis 配置 mybatis: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl server: port: 8090 servlet: jsp: init-parameters: development: true