基于SpringBoot、Mybatis、Druid实现一个服务连接多个数据库的动态数据源代码,本文中数据源是固定两个,在配置文件和代码中写死成两个了,如果需要更多可以修改配置和代码,更灵活的方式是把数据源写成数组,这样代码就不用改多少。
可以看出代码中的数据源配置来源是配置文件,那其实通过接口等方式动态控制数据源和运行中的代码也是可以的,客官可以自己实现。
配置文件
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initialSize=8
spring.datasource.druid.minIdle=8
spring.datasource.druid.maxActive=50
spring.datasource.druid.maxWait=60000
spring.datasource.druid.datasource1.name=datasource1
spring.datasource.druid.datasource1.url=jdbc:***
spring.datasource.druid.datasource1.username=***
spring.datasource.druid.datasource1.password=***
spring.datasource.druid.datasource1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.datasource2.name=datasource2
spring.datasource.druid.datasource2.url=jdbc:***
spring.datasource.druid.datasource2.username=***
spring.datasource.druid.datasource2.password=***
spring.datasource.druid.datasource2.driver-class-name=com.mysql.cj.jdbc.Driver
配置读取类
import lombok.Data;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.druid")
public class DatasourceProperties {
private Integer initialSize;
private Integer minIdle;
private Integer maxActive;
private Long maxWait;
private DataSourceProperties datasource1;
private DataSourceProperties datasource2;
}
动态数据源配置类
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.util.HashMap;
@Configuration
public class MyDynamicDatasource {
@Autowired
DatasourceProperties datasourceProperties;
@Bean
public MyRoutingDataSource dynamicDataSource() {
HashMap<Object, Object> m = new HashMap<>();
DataSource datasource1= getDatasource(datasourceProperties, datasourceProperties.getDatasource1());
m.put("datasource1", datasource1);
DataSource datasource2= getDatasource(datasourceProperties, datasourceProperties.getDatasource2());
m.put("datasource2", datasource2);
MyRoutingDataSource myRoutingDataSource = new MyRoutingDataSource();
myRoutingDataSource.setDefaultTargetDataSource(datasource1);
myRoutingDataSource.setTargetDataSources(m);
return myRoutingDataSource;
}
private DataSource getDatasource(DatasourceProperties datasourceProperties, DataSourceProperties datasource) {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setInitialSize(datasourceProperties.getInitialSize());
druidDataSource.setMinIdle(datasourceProperties.getMinIdle());
druidDataSource.setMaxActive(datasourceProperties.getMaxActive());
druidDataSource.setMaxWait(datasourceProperties.getMaxWait());
druidDataSource.setUrl(datasource.getUrl());
druidDataSource.setUsername(datasource.getUsername());
druidDataSource.setPassword(datasource.getPassword());
druidDataSource.setDriverClassName(datasource.getDriverClassName());
return druidDataSource;
}
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
路由实现
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class MyRoutingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceKeyHolder.getKey();
}
}
数据源key工具
public class DataSourceKeyHolder {
private static final ThreadLocal<String> keyHolder = new ThreadLocal<>();
public static void setKey(String key) {
keyHolder.set(key);
}
public static String getKey() {
String s = keyHolder.get();
return s;
}
public static void clear(){
keyHolder.remove();
}
}
动态数据源切换拦截器
import com.config.dynamicdatasource.DataSourceKeyHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.ParamNameResolver;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;
@Intercepts({
@Signature(type = Executor.class, method = "update",
args = {MappedStatement.class, Object.class}),
@Signature(type = Executor.class, method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
})
@Slf4j
@Component
public class DynamicallyDataSourceInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
if (args[0] instanceof MappedStatement) {
MappedStatement ms = (MappedStatement) args[0];
BoundSql boundSql = ms.getBoundSql(wrapCollection(args[1]));
String sql = boundSql.getSql();
if (sql.contains("这边就按各个数据源的表名特征填字符串识别,如表名前缀")) {
DataSourceKeyHolder.setKey("datasource1");
} else {
DataSourceKeyHolder.setKey("datasource2");
}
}
return invocation.proceed();
}
private Object wrapCollection(final Object object) {
return ParamNameResolver.wrapToMapIfCollection(object, null);
}
}