Search in sources :

Example 1 with DataSourceProperty

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);
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource) DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource) DataSource(javax.sql.DataSource)

Example 2 with DataSourceProperty

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;
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet)

Example 3 with DataSourceProperty

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());
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource) DbInfo(com.maxqiu.demo.system.entity.DbInfo) DataSource(javax.sql.DataSource) DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource)

Example 4 with DataSourceProperty

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;
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) DataSource(javax.sql.DataSource)

Example 5 with DataSourceProperty

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;
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) Statement(java.sql.Statement) Connection(java.sql.Connection) SQLException(java.sql.SQLException)

Aggregations

DataSourceProperty (com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty)23 DataSource (javax.sql.DataSource)9 DynamicRoutingDataSource (com.baomidou.dynamic.datasource.DynamicRoutingDataSource)7 HashMap (java.util.HashMap)7 ResultSet (java.sql.ResultSet)5 DataSourceConfig (com.hccake.ballcat.codegen.model.entity.DataSourceConfig)3 Statement (java.sql.Statement)3 AbstractJdbcDataSourceProvider (com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider)2 DynamicDataSourceProperties (com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties)2 HikariDataSource (com.zaxxer.hikari.HikariDataSource)2 Bean (org.springframework.context.annotation.Bean)2 DbInfo (com.maxqiu.demo.system.entity.DbInfo)1 GenDsInfo (com.xhuicloud.common.datasource.entity.GenDsInfo)1 DsJdbcUrlEnum (com.xhuicloud.common.datasource.enums.DsJdbcUrlEnum)1 HikariConfig (com.zaxxer.hikari.HikariConfig)1 ApiOperation (io.swagger.annotations.ApiOperation)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Map (java.util.Map)1 SneakyThrows (lombok.SneakyThrows)1