Spring Boot application integrates dynamic DataSource quickly.
- Import dependencies
<dependency>
<groupId>cn.unikue.springstarter</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>LATEST</version>
</dependency>By default, this starter will auto take effect, you can turn it off by
spring.dynamic-datasource.enabled = false
- Configure Spring Boot
application.ymlwith prefixspring.dynamic-datasource
spring:
dynamic-datasource:
datasource-route:
testdb1:
url: 'jdbc:mysql://127.0.0.1:3306/test_db1'
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
testdb2:
url: 'jdbc:mysql://127.0.0.1:3306/test_db2'
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.mchange.v2.c3p0.ComboPooledDataSource
testdb3:
url: 'jdbc:mysql://127.0.0.1:3306/test_db3'
driver-class-name: com.mysql.cj.jdbc.Driver
type: org.apache.commons.dbcp2.BasicDataSourceThe identifiers (
testdb1,testdb2,testdb3) are used as keys for switching severalDataSource, they could be any names if you like, just attention that, these names must be alphanumeric.
- Annotate your (non-static) method with
@DataSourceRoutingannotation, done! Take one of the previous databasetestdb1for example, the annotation is
@DataSourceRouting(key = "testdb1")
Note that the
keysegment, that is the database identifier under yourdatasource-routenode of the previous step.
-
Optional feature: You can also define your databases in code (not just the
application.properties), what you have to do is declaringDataSourcebeans with the@DataSourceDefinitionannotation. -
This starter supports the most popular data source pools in the word, including
- c3p0
- dbcp2
- druid
- hikari
- oracle ucp
- tomcat
- vibur
- jdk 17+
This project is under the Apache License 2.0
See the NOTICE.txt file for required notices and attributions.
You like this package? Then donate to us to support the development.
Beijing Unikue Network Technology Ltd.
- Unikue: https://unikue.cn