Search in sources :

Example 6 with DataSourceProperty

use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project ballcat-codegen by ballcat-projects.

the class DataSourceConfigServiceImpl method save.

@Override
public boolean save(DataSourceConfigDTO dto) {
    // 新的数据源配置信息
    DataSourceProperty dataSourceProperty = dynamicDataSourceHelper.prodDataSourceProperty(dto.getDsKey(), dto.getUrl(), dto.getUsername(), dto.getPass());
    // 校验数据源配置
    if (dynamicDataSourceHelper.isErrorDataSourceProperty(dataSourceProperty)) {
        return false;
    }
    // 转换为实体,并将密码加密
    DataSourceConfig dataSourceConfig = DataSourceConfigConverter.INSTANCE.dtoToPo(dto);
    String pass = dto.getPass();
    dataSourceConfig.setPassword(dynamicDataSourceHelper.encryptPass(pass));
    // 落库存储
    int flag = baseMapper.insert(dataSourceConfig);
    if (!SqlHelper.retBool(flag)) {
        return false;
    }
    // 动态添加数据源
    dynamicDataSourceHelper.addDynamicDataSource(dataSourceProperty);
    return true;
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) DataSourceConfig(com.hccake.ballcat.codegen.model.entity.DataSourceConfig)

Example 7 with DataSourceProperty

use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project ballcat-codegen by ballcat-projects.

the class DynamicDataSourceHelper method prodDataSourceProperty.

/**
 * 获得数据源配置实体
 * @param dsName 数据源名称
 * @param url 数据库连接
 * @param username 数据库用户名
 * @param password 数据库密码
 * @return 数据源配置
 */
public DataSourceProperty prodDataSourceProperty(String dsName, String url, String username, String password) {
    DataSourceProperty dataSourceProperty = new DataSourceProperty();
    dataSourceProperty.setPoolName(dsName);
    dataSourceProperty.setUrl(url);
    dataSourceProperty.setUsername(username);
    dataSourceProperty.setPassword(password);
    return dataSourceProperty;
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty)

Example 8 with DataSourceProperty

use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project ballcat-codegen by ballcat-projects.

the class DynamicJdbcDataSourceLoader method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    // 查找所有配置的生成项目使用数据源
    List<DataSourceConfig> list = dataSourceConfigService.list();
    // 遍历添加进动态数据源中
    for (DataSourceConfig dataSourceConfig : list) {
        String dsName = dataSourceConfig.getDsKey();
        String username = dataSourceConfig.getUsername();
        String password = dynamicDataSourceHelper.decryptPassword(dataSourceConfig.getPassword());
        String url = dataSourceConfig.getUrl();
        DataSourceProperty property = dynamicDataSourceHelper.prodDataSourceProperty(dsName, url, username, password);
        // 如果数据源异常,则不加载
        if (!dynamicDataSourceHelper.isErrorDataSourceProperty(property)) {
            dynamicDataSourceHelper.addDynamicDataSource(property);
        }
    }
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) DataSourceConfig(com.hccake.ballcat.codegen.model.entity.DataSourceConfig)

Example 9 with DataSourceProperty

use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project XHuiCloud by sindaZeng.

the class XHuiDynamicDataSourceProvider method executeStmt.

/**
 * 执行语句获得数据源参数
 * @param statement 语句
 * @return 数据源参数
 * @throws SQLException sql异常
 */
@SneakyThrows
@Override
protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
    ResultSet rs = statement.executeQuery(properties.getQueryDsSql());
    Map<String, DataSourceProperty> map = new HashMap<>(8);
    List<GenDsInfo> sysTenantDs = populate(rs, GenDsInfo.class);
    for (GenDsInfo ds : sysTenantDs) {
        String url;
        DsJdbcUrlEnum dsJdbcUrlEnum = DsJdbcUrlEnum.get(ds.getType());
        if (DsJdbcUrlEnum.MSSQL == dsJdbcUrlEnum) {
            url = String.format(dsJdbcUrlEnum.getUrl(), ds.getHost(), ds.getPort(), ds.getName());
        } else {
            url = String.format(dsJdbcUrlEnum.getUrl(), ds.getHost(), ds.getPort(), ds.getName());
        }
        DataSourceProperty property = new DataSourceProperty();
        property.setUsername(ds.getUsername());
        property.setPassword(ds.getPassword());
        property.setUrl(url);
        map.put(ds.getName(), property);
    }
    // 添加默认主数据源
    DataSourceProperty property = new DataSourceProperty();
    property.setUsername(properties.getUsername());
    property.setPassword(properties.getPassword());
    property.setUrl(properties.getUrl());
    map.put("master", property);
    return map;
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) GenDsInfo(com.xhuicloud.common.datasource.entity.GenDsInfo) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) DsJdbcUrlEnum(com.xhuicloud.common.datasource.enums.DsJdbcUrlEnum) SneakyThrows(lombok.SneakyThrows)

Example 10 with DataSourceProperty

use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project ruoyi-vue-pro by YunaiV.

the class DbDocController method buildDataSource.

/**
 * 创建数据源
 */
// TODO 芋艿:screw 暂时不支持 druid,尴尬
private HikariDataSource buildDataSource() {
    // 获得 DataSource 数据源,目前只支持首个
    String primary = dynamicDataSourceProperties.getPrimary();
    DataSourceProperty dataSourceProperty = dynamicDataSourceProperties.getDatasource().get(primary);
    // 创建 HikariConfig 配置类
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setJdbcUrl(dataSourceProperty.getUrl());
    hikariConfig.setUsername(dataSourceProperty.getUsername());
    hikariConfig.setPassword(dataSourceProperty.getPassword());
    // 设置可以获取 tables remarks 信息
    hikariConfig.addDataSourceProperty("useInformationSchema", "true");
    // 创建数据源
    return new HikariDataSource(hikariConfig);
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariConfig(com.zaxxer.hikari.HikariConfig)

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