Dynamic-Datasource动态数据源

1、添加请求对应的数据源标签

DynamicDataSourceContextHolder.push(ds);

2、添加数据源

 

 3、动态添加数据源

private DynamicRoutingDataSource dataSource;
private DefaultDataSourceCreator dataSourceCreator;

//创建数据源
DataSourceProperty dataSourceProperty = new DataSourceProperty();
dataSourceProperty.setUrl(dbInfo.getDbUrl());
dataSourceProperty.setUsername(dbInfo.getDbUsername());
dataSourceProperty.setPassword(dbInfo.getDbPassword());
dataSourceProperty.setDriverClassName("com.mysql.cj.jdbc.Driver");

dataSourceProperty.setDruid(new DruidConfig());
return this.dataSourceCreator.createDataSource(dataSourceProperty);

 //动态添加数据源
 this.dataSource.addDataSource(tenantId, dataSource);

 

posted @ 2024-05-16 11:08  扰扰  阅读(93)  评论(0编辑  收藏  举报