# dynamic-datasource **Repository Path**: li-dongfang/dynamic-datasource ## Basic Information - **Project Name**: dynamic-datasource - **Description**: 基于spring boot 3,mybatis和druid的多数据源动态切换和多数据源事务统一管理。 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-04-05 - **Last Updated**: 2023-04-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # dynamicDatasource dynamicDatasource 介绍 基于spring boot 3,mybatis和druid数据源的多数据源动态切换和多数据源事务统一管理。 使用教程 1. 创建spring boot项目 2. 启动类关闭druid数据源的自动装载 @EnableDynamicTransactionManagement @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public void main{....} 3.在项目的resources目录下创建dynamic-datasource.yaml,并提供如下基础配置项,如需添加更多的配置项,可在DbConfig类中添加相应的属性配置 提供配置项如下: ``` dynamic: datasource: #包扫描路径,不支持分包处理 mapper-location: classpath*:mapper/*/*.xml db-properties: - db-definition: db-name: sys primary: true dbConfig: url: jdbc:mysql://localhost:3306/admin-app?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC username: root password: root123456 driver-class-name: com.mysql.cj.jdbc.Driver async-init: true initial-size: 5 min-idle: 5 max-active: 20 max-wait: 60000 #多长时间检测需要关闭的空闲连接 time-between-eviction-runs-millis: 60000 #连接池中的最小生存时间 min-evictable-idle-time-millis: 30000 #最大生存时间 max-evictable-idle-time-millis: 600000 #如果配置了此项,数据库中应该存在这个表,否则会直接报当前的表不存在的错误 validation-query: SELECT 1 FROM test test-while-idle: true test-on-borrow: true test-on-return: false # 是否缓存preparedStatement PsCache pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 #数据库的用户名密码设置 - db-definition: db-name: business primary: false dbConfig: url: jdbc:mysql://localhost:3306/admin-business?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC username: root password: root123456 driver-class-name: com.mysql.cj.jdbc.Driver async-init: true initial-size: 5 min-idle: 5 max-active: 20 max-wait: 60000 #多长时间检测需要关闭的空闲连接 time-between-eviction-runs-millis: 60000 #连接池中的最小生存时间 min-evictable-idle-time-millis: 30000 #最大生存时间 max-evictable-idle-time-millis: 600000 validation-query: SELECT 1 FROM TEST test-while-idle: true test-on-borrow: true test-on-return: false # 是否缓存preparedStatement PsCache pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 ``` 相应的jar包和配置文件已经放到resources下面 4. 配置完成之后,进行mybatis的项目书写,数据源默认为标志primary为true的数据源,如果有多个会按照primary的属性设置为最后一个处理。 5.动态数据源切换,@DataSource("数据源名称")。 数据源名称即配置文件中Dbdefinition使用的名称,需要保证该注解添加在Mapper接口或者接口中定义的方法上,以保证其可以正常拦截mybatis的Mapper接口和Mapper中提供的方法 6.@DTransactional注解。该注解为多数据源统一事务注解,不与spring boot默认注解@Transactional一起使用。使用该注解之后,事务会进行多段的提交和回滚,保证多数据源事务的一致性。 7.特别声明:此仅为学习代码所书写,目前支持的多数据源自动切换是基于Druid数据源,druid数据源不支持的也不支持。如果需要线上使用,请事先搭建本地环境进行使用。 使用说明 1. 使用前可以自行整合相关源代码也可以使用已经打包之后的starter文件 2. 使用前请务必核实,是否存在一些未知bug 3. 如存在bug欢迎提出并改正。 参与贡献 其他 1. 测试用例可参考--- 2.更多有意思的小东西,请浏览GITEE或者GITHUB等优秀开源网站