https://www.cnblogs.com/aizen-sousuke/p/11756279.html
使用注解方式
依赖
spring-boot-starter-web
mysql-connector-java
dynamic-datasource-spring-boot-starter #多数据源配置主要依赖
druid-spring-boot-starter
application.yml
server: prot: 8080 spring: # 缓存配置 cache: type: redis # redis 单机环境配置 redis: host: 192.168.8.240 port: 6379 password: 123456 ssl: false jedis: pool: max-active: 30 max-wait: 1 max-idle: 15 min-idle: 0 timeout: 60000 # 数据源设置 datasource: dynamic: primary: mysqlDS1 #指定默认加载数据源 datasource: mysqlDS1: url: jdbc:mysql://192.168.8.240:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mysqlDS2: url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver druid: intinal-size: 5 max-active: 20 min-idle: 5 max-wait: 60000
com.mysql.cj.jdbc.Driver和 com.mysql.jdbc.Driver区别
BootApplication
@SpringBootApplication (exclude = DruidDataSourceAutoConfigure.class)
@MapperScan("com.anc.**.mapper")
@EnableCaching
public class BootApplication {
public static void main(String[] args) {
SpringApplication.run(BootApplication.class, args);
}
}
非默认数据源调用时加上@DS,@DS 可以注解在方法上和类上,同时存在方法注解优先于类上注解。