use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project jeecg-boot by jeecgboot.
the class CommonUtils method getDataSourceProperty.
/**
* 根据数据源key获取DataSourceProperty
* @param sourceKey
* @return
*/
public static DataSourceProperty getDataSourceProperty(String sourceKey) {
DynamicDataSourceProperties prop = SpringContextUtils.getApplicationContext().getBean(DynamicDataSourceProperties.class);
Map<String, DataSourceProperty> map = prop.getDatasource();
DataSourceProperty db = (DataSourceProperty) map.get(sourceKey);
return db;
}
use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project jeecg-boot by jeecgboot.
the class CommonUtils method getDataSourceConnect.
/**
* 根据sourceKey 获取数据源连接
* @param sourceKey
* @return
* @throws SQLException
*/
public static Connection getDataSourceConnect(String sourceKey) throws SQLException {
if (oConvertUtils.isEmpty(sourceKey)) {
sourceKey = "master";
}
DynamicDataSourceProperties prop = SpringContextUtils.getApplicationContext().getBean(DynamicDataSourceProperties.class);
Map<String, DataSourceProperty> map = prop.getDatasource();
DataSourceProperty db = (DataSourceProperty) map.get(sourceKey);
if (db == null) {
return null;
}
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(db.getDriverClassName());
ds.setUrl(db.getUrl());
ds.setUsername(db.getUsername());
ds.setPassword(db.getPassword());
return ds.getConnection();
}
use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project XHuiCloud by sindaZeng.
the class GenDsInfoServiceImpl method addDynamicDataSource.
@Override
public Boolean addDynamicDataSource(GenDsInfo genDsInfo) {
DataSourceProperty dataSourceProperty = new DataSourceProperty();
dataSourceProperty.setPoolName(genDsInfo.getName());
dataSourceProperty.setUrl(String.format(DsJdbcUrlEnum.get(genDsInfo.getType()).getUrl(), genDsInfo.getHost(), genDsInfo.getPort(), genDsInfo.getName()));
dataSourceProperty.setUsername(genDsInfo.getUsername());
dataSourceProperty.setPassword(genDsInfo.getPassword());
DataSource dataSource = druidDataSourceCreator.createDataSource(dataSourceProperty);
DynamicRoutingDataSource dynamicRoutingDataSource = SpringUtil.getBean(DynamicRoutingDataSource.class);
dynamicRoutingDataSource.addDataSource(dataSourceProperty.getPoolName(), dataSource);
return Boolean.TRUE;
}
use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project springboot-templet-start by thedestiny.
the class DataSourceController method fillSourceProperty.
private DataSourceProperty fillSourceProperty(DataSourceDTO dto) {
DataSourceProperty dataSourceProperty = new DataSourceProperty();
BeanUtils.copyProperties(dto, dataSourceProperty);
return dataSourceProperty;
}
use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project dynamic-datasource-samples by dynamic-datasource.
the class LoadDsApplication method dynamicDataSourceProvider.
@Bean
public DynamicDataSourceProvider dynamicDataSourceProvider() {
return new AbstractJdbcDataSourceProvider("org.h2.Driver", "jdbc:h2:mem:test", "sa", "") {
@Override
protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
// *************** 实际运行应直接从库中查出,演示所以需要先插入数据***************
statement.execute("CREATE TABLE IF NOT EXISTS `DB`\n" + "(\n" + " `name` VARCHAR(30) NULL DEFAULT NULL,\n" + " `username` VARCHAR(30) NULL DEFAULT NULL,\n" + " `password` VARCHAR(30) NULL DEFAULT NULL,\n" + " `url` VARCHAR(30) NULL DEFAULT NULL,\n" + " `driver` VARCHAR(30) NULL DEFAULT NULL\n" + ")");
statement.executeUpdate("insert into DB values ('db1','sa','','jdbc:h2:mem:test2','org.h2.Driver')");
statement.executeUpdate("insert into DB values ('db2','sa','','jdbc:h2:mem:test3','org.h2.Driver')");
statement.executeUpdate("insert into DB values ('db3','sa','','jdbc:h2:mem:test4','org.h2.Driver')");
Map<String, DataSourceProperty> map = new HashMap<>();
// *************** ↑↑↑↑↑↑↑ END ***************
ResultSet rs = statement.executeQuery("select * from DB");
while (rs.next()) {
String name = rs.getString("name");
String username = rs.getString("username");
String password = rs.getString("password");
String url = rs.getString("url");
String driver = rs.getString("driver");
DataSourceProperty property = new DataSourceProperty();
property.setUsername(username);
property.setPassword(password);
property.setUrl(url);
property.setDriverClassName(driver);
map.put(name, property);
}
return map;
}
};
}
Aggregations