首先在yml文件配置多个数据源
spring: datasource: dynamic: # 是否开启 SQL日志输出,生产环境建议关闭,有性能损耗 p6spy: true hikari: connection-timeout: 30000 max-lifetime: 1800000 max-pool-size: 15 min-idle: 5 connection-test-query: select 1 pool-name: FebsHikariCP # 配置默认数据源 primary: base datasource: # 数据源-1,名称为 base base: username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/linFebs?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8 # 数据源-2,名称为 quartz zh: username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/anji_zh?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8
然后在实现类用注解 @DS(“”) 标识是哪个数据源
/*** 灾害系统主体(ZhSubject)表服务实现类** @author lin* @since 2021-04-27 11:03:55*/"zhSubjectService") ("zh") (publicclassZhSubjectServiceImplextendsServiceImpl<ZhSubjectMapper, ZhSubject>implementsZhSubjectService { }
启动程序访问 数据源链接成功
获取到结果