Search in sources :

Example 1 with DynamicDataSourceProperties

use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties 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;
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) DynamicDataSourceProperties(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties)

Example 2 with DynamicDataSourceProperties

use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties 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();
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) DynamicDataSourceProperties(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties)

Aggregations

DataSourceProperty (com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty)2 DynamicDataSourceProperties (com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties)2 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)1