1.主从数据源配置(读写分离)
参考链接:
https://gitee.com/comven/dynamic-datasource-demo?spm=5176.100239.blogcont188540.13.iARYDh
https://blog.csdn.net/qq_35981283/article/details/78846892
2数据库信息需要动态加载(每一个线程根据自己的实际参数设置数据源)
参考链接:
https://blog.csdn.net/c598976342/article/details/82893993
(1)pom jar包引入
<!--spring boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!--spring boot mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!--spring boot AOP (使用切面的方式需要添加)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--spring boot jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
(2)application.yml 设置数据源
#数据库配置
hikari:
#默认数据库
-master:
username: root
password: aj
jdbc-url: jdbc:mysql://localhost:3306/task_manage?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
maximum-pool-size: 10
pool-name: master(localhost)
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 1765000
data-source-properties:
cachePrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
useServerPrepStmts: true
useLocalSessionState: true
useLocalTransactionState: true
rewriteBatchedStatements: true
cacheResultSetMetadata: true
cacheServerConfiguration: true
elideSetAutoCommits: true
maintainTimeStats: false
# 通用数据库
-common:
username: root
password: aj
jdbc-url: jdbc:mysql://localhost:3306/task_%s?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
(3)创建线程数据源信息存储类
DynamicDataSourceHolder.java
pac