# dynamic-datasource-spring-boot-starter
**Repository Path**: ChuckChan/dynamic-datasource-spring-boot-starter
## Basic Information
- **Project Name**: dynamic-datasource-spring-boot-starter
- **Description**: 基于 springBoot2.0 多数据源 动态数据源 快速启动器
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 12
- **Forks**: 596
- **Created**: 2018-05-09
- **Last Updated**: 2024-05-29
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# dynamic-datasource-spring-boot-starter
[](https://www.travis-ci.org/baomidou/dynamic-datasource-spring-boot-starter)
[](http://mvnrepository.com/artifact/com.baomidou/dynamic-datasource-spring-boot-starter)
[](http://www.apache.org/licenses/LICENSE-2.0.html)
#### [Github](https://github.com/baomidou/dynamic-datasource-spring-boot-starter) | [Gitee](https://gitee.com/baomidou/dynamic-datasource-spring-boot-starter)
## [English introduction](README.md) | [中文说明](README_zh.md)
## Introduction
dynamic-datasource-spring-boot-starter based on springBoot2.0.
It can be used for Separation of reading and writing.
The master database to `INSERT` `UPDATE` `DELETE` .
The slave database to `SELECT` .
If you project is complex,I will propose you use [sharding-jdbc ](https://github.com/shardingjdbc/sharding-jdbc).
## Demo
[dynamic-datasource-example](https://gitee.com/baomidou/dynamic-datasource-example) a simple dynamic project that u can direct run。
## How to use
1. Import dynamic-datasource-spring-boot-starter。
```xml
com.baomidou
dynamic-datasource-spring-boot-starter
${version}
```
2. Config your master-slave info in application.yml。
spring.datasource.dynamic.master (config the unique master database)
spring.datasource.dynamic.slave (config every slave database)
```yaml
spring:
datasource:
dynamic:
master:
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://47.100.20.186:3307/dynamic?characterEncoding=utf8&useSSL=false
slave:
one:
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://47.100.20.186:3308/dynamic?characterEncoding=utf8&useSSL=false
two:
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://47.100.20.186:3309/dynamic?characterEncoding=utf8&useSSL=false
```
3. Use annotation `@DS` to switch database.
> `@DS` can be annotation on serviceImpl method or mybatis mapper method.
| annotation | result |
| :---------: | :----------------------------------------------------------: |
| without @DS | master |
| @DS("one") | If exist a database named 'one' then switch to one,otherwise to master |
| @DS | depensOn DynamicDataSourceStrategy to determine a slave database,the default strategy is LoadBalance. |
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
@DS("one")
public List