Search in sources :

Example 21 with DataSourceProperty

use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project longmarch by yuyueqty.

the class JdbcDataSourceConfig method jdbcDynamicDataSourceProvider.

@Bean
public DynamicDataSourceProvider jdbcDynamicDataSourceProvider() {
    return new AbstractJdbcDataSourceProvider(driver, url, username, password) {

        @Override
        protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
            ResultSet rs = statement.executeQuery("SELECT * FROM " + DATA_SOURCE_INFO);
            Map<String, DataSourceProperty> map = new HashMap<>(8);
            while (rs.next()) {
                String name = rs.getString("data_source_name");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String url = rs.getString("url");
                String driver = rs.getString("driver_class_name");
                if (StrUtil.isBlank(name) || StrUtil.isBlank(username) || StrUtil.isBlank(password) || StrUtil.isBlank(url) || StrUtil.isBlank(driver)) {
                    continue;
                }
                DataSourceProperty property = new DataSourceProperty();
                property.setPoolName(name);
                property.setUsername(username);
                property.setPassword(password);
                property.setUrl(url);
                property.setDriverClassName(driver);
                map.put(name, property);
            }
            return map;
        }
    };
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) HashMap(java.util.HashMap) Statement(java.sql.Statement) AbstractJdbcDataSourceProvider(com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider) ResultSet(java.sql.ResultSet) Bean(org.springframework.context.annotation.Bean)

Example 22 with DataSourceProperty

use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project longmarch by yuyueqty.

the class DataSourceController method add.

@ApiOperation(value = "添加数据源")
@PostMapping("/add")
public Set<String> add(@Validated @RequestBody DataSourceDTO dto) {
    DataSourceProperty dataSourceProperty = new DataSourceProperty();
    BeanUtils.copyProperties(dto, dataSourceProperty);
    DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
    DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
    ds.addDataSource(dto.getPoolName(), dataSource);
    return ds.getDataSources().keySet();
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource) DataSource(javax.sql.DataSource) DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource) ApiOperation(io.swagger.annotations.ApiOperation)

Example 23 with DataSourceProperty

use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project helio-starters by uncarbon97.

the class HelioDynamicDataSourceRegistry method containsDataSource.

/**
 * 查询是否已存在数据源
 *
 * @param dataSourceName 数据源名称
 * @param obtainIfAbsent 缺失时尝试从 IoC 容器中获取,并加入数据源缓存
 * @return boolean
 */
public boolean containsDataSource(String dataSourceName, boolean obtainIfAbsent) {
    /*
        配置不存在则动态添加数据源,以懒加载的模式解决分布式场景的配置同步
        为了保证数据完整性,配置后生成数据源缓存,后台便无法修改更换数据源,若一定要修改请迁移数据后重启服务或自行修改底层逻辑
         */
    if (dynamicRoutingDataSource.getDataSources().containsKey(dataSourceName)) {
        return true;
    }
    if (!obtainIfAbsent) {
        return false;
    }
    boolean successFlag = false;
    for (DataSourceDefinitionProvider provider : dataSourceDefinitionProviders) {
        DataSourceDefinition dataSourceDefinition = provider.getDataSourceDefinition(dataSourceName);
        if (dataSourceDefinition != null) {
            // 存在非空结果,则拷贝数据源配置
            DataSourceProperty dataSourceProperty = new DataSourceProperty();
            BeanUtil.copyProperties(dataSourceDefinition, dataSourceProperty);
            // 补充字段
            dataSourceProperty.setPoolName(dataSourceDefinition.getName()).setType(HikariDataSource.class);
            // 创建动态数据源
            DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
            // 添加新数据源
            dynamicRoutingDataSource.addDataSource(dataSourceName, dataSource);
            log.info("已动态添加新数据源 {}", dataSourceName);
            successFlag = true;
            break;
        }
    }
    if (!successFlag) {
        log.error("动态添加新数据源失败,原因:没有 DataSourceDefinitionProvider 提供 {} 的 DataSourceDefinition", dataSourceName);
    }
    return successFlag;
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource) DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource)

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