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 .
dynamic-datasource-example a simple dynamic project that u can direct run。
- Import dynamic-datasource-spring-boot-starter。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
- Config your master-slave info in application.yml。
spring.datasource.dynamic.master (config the unique master database)
spring.datasource.dynamic.slaves (config every slave database)
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
slaves:
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
- 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. |
@Service
public class UserServiceImpl implements UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
@DS("one")
public List<Map<String, Object>> selectAll() {
return jdbcTemplate.queryForList("select * from user");
}
@Override
@DS
public List<Map<String, Object>> selectByCondition() {
return jdbcTemplate.queryForList("select * from user where age >10");
}
}
In mybatis environment it can also annotation on mapper method.
public interface UserMapper {
@Insert("INSERT INTO user (name,age) values (#{name},#{age})")
boolean addUser(@Param("name") String name, @Param("age") Integer age);
@Update("UPDATE user set name=#{name}, age=#{age} where id =#{id}")
boolean updateUser(@Param("id") Integer id, @Param("name") String name, @Param("age") Integer age);
@Delete("DELETE from user where id =#{id}")
boolean deleteUser(@Param("id") Integer id);
@Select("SELECT * FROM user")
@DS
List<User> selectAll();
}
- Config DynamicDataSourceProvider .
The default DynamicDataSourceProvider is YmlDynamicDataSourceProvider
,It read database info from application.yaml
or application.properties
.
public interface DynamicDataSourceProvider {
/**
* load master
*
* @return masterDataSource
*/
DataSource loadMasterDataSource();
/**
* load slaves
*
* @return slaveDataSource
*/
Map<String, DataSource> loadSlaveDataSource();
}
- Config DynamicDataSourceStrategy.
The default DynamicDataSourceStrategy is LoadBalanceDynamicDataSourceStrategy
.
The starter alse provide RandomDynamicDataSourceStrategy
.
public interface DynamicDataSourceStrategy {
/**
* determine a slaveId
*
* @param slaveDataSourceLookupKeys slaveKeys
* @return slaveId
*/
String determineSlaveDataSource(String[] slaveDataSourceLookupKeys);
}
Demo to use RandomDynamicDataSourceStrategy
instead of LoadBalanceDynamicDataSourceStrategy
.
@Configuration
public class DynamicConfiguration {
@Bean
public DynamicDataSourceStrategy dynamicDataSourceStrategy() {
return new RandomDynamicDataSourceStrategy();
}
}