use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project pig by pig-mesh.
the class GenDatasourceConfServiceImpl method addDynamicDataSource.
/**
* 添加动态数据源
* @param conf 数据源信息
*/
@Override
public void addDynamicDataSource(GenDatasourceConf conf) {
DataSourceProperty dataSourceProperty = new DataSourceProperty();
dataSourceProperty.setPoolName(conf.getName());
dataSourceProperty.setUrl(conf.getUrl());
dataSourceProperty.setUsername(conf.getUsername());
dataSourceProperty.setPassword(conf.getPassword());
dataSourceProperty.setLazy(true);
DataSource dataSource = hikariDataSourceCreator.createDataSource(dataSourceProperty);
SpringContextHolder.getBean(DynamicRoutingDataSource.class).addDataSource(dataSourceProperty.getPoolName(), dataSource);
}
use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project pig by pig-mesh.
the class JdbcDynamicDataSourceProvider method executeStmt.
/**
* 执行语句获得数据源参数
* @param statement 语句
* @return 数据源参数
* @throws SQLException sql异常
*/
@Override
protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
ResultSet rs = statement.executeQuery(properties.getQueryDsSql());
Map<String, DataSourceProperty> map = new HashMap<>(8);
while (rs.next()) {
String name = rs.getString(DataSourceConstants.DS_NAME);
String username = rs.getString(DataSourceConstants.DS_USER_NAME);
String password = rs.getString(DataSourceConstants.DS_USER_PWD);
String url = rs.getString(DataSourceConstants.DS_JDBC_URL);
DataSourceProperty property = new DataSourceProperty();
property.setUsername(username);
property.setLazy(true);
property.setPassword(stringEncryptor.decrypt(password));
property.setUrl(url);
map.put(name, property);
}
// 添加默认主数据源
DataSourceProperty property = new DataSourceProperty();
property.setUsername(properties.getUsername());
property.setPassword(properties.getPassword());
property.setUrl(properties.getUrl());
property.setLazy(true);
map.put(DataSourceConstants.DS_MASTER, property);
return map;
}
use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project demo-SpringBoot by Max-Qiu.
the class ApplicationRunnerImpl method run.
@Override
public void run(ApplicationArguments args) {
List<DbInfo> list = dbInfoService.list();
for (DbInfo dbInfo : list) {
DataSourceProperty dataSourceProperty = new DataSourceProperty();
dataSourceProperty.setDriverClassName(dbInfo.getDriverClassName());
dataSourceProperty.setUrl(dbInfo.getUrl());
dataSourceProperty.setUsername(dbInfo.getUsername());
dataSourceProperty.setPassword(dbInfo.getPassword());
dataSourceProperty.setLazy(true);
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
DataSource dataSource = hikariDataSourceCreator.createDataSource(dataSourceProperty);
ds.addDataSource(dbInfo.getKey(), dataSource);
}
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
System.out.println(ds.getDataSources().keySet());
}
use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project dynamic-datasource-spring-boot-starter by baomidou.
the class AbstractDataSourceProvider method createDataSourceMap.
protected Map<String, DataSource> createDataSourceMap(Map<String, DataSourceProperty> dataSourcePropertiesMap) {
Map<String, DataSource> dataSourceMap = new HashMap<>(dataSourcePropertiesMap.size() * 2);
for (Map.Entry<String, DataSourceProperty> item : dataSourcePropertiesMap.entrySet()) {
String dsName = item.getKey();
DataSourceProperty dataSourceProperty = item.getValue();
String poolName = dataSourceProperty.getPoolName();
if (poolName == null || "".equals(poolName)) {
poolName = dsName;
}
dataSourceProperty.setPoolName(poolName);
dataSourceMap.put(dsName, defaultDataSourceCreator.createDataSource(dataSourceProperty));
}
return dataSourceMap;
}
use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project dynamic-datasource-spring-boot-starter by baomidou.
the class AbstractJdbcDataSourceProvider method loadDataSources.
@Override
public Map<String, DataSource> loadDataSources() {
Connection conn = null;
Statement stmt = null;
try {
// 但在用户显示配置的情况下,进行主动加载
if (!StringUtils.isEmpty(driverClassName)) {
Class.forName(driverClassName);
log.info("成功加载数据库驱动程序");
}
conn = DriverManager.getConnection(url, username, password);
log.info("成功获取数据库连接");
stmt = conn.createStatement();
Map<String, DataSourceProperty> dataSourcePropertiesMap = executeStmt(stmt);
return createDataSourceMap(dataSourcePropertiesMap);
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.closeConnection(conn);
JdbcUtils.closeStatement(stmt);
}
return null;
}
Aggregations