Search in sources :

Example 11 with DynamicRoutingDataSource

use of com.baomidou.dynamic.datasource.DynamicRoutingDataSource 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;
}
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 12 with DynamicRoutingDataSource

use of com.baomidou.dynamic.datasource.DynamicRoutingDataSource in project XHuiCloud by sindaZeng.

the class GenDsInfoServiceImpl method updateDynamicDataSource.

@Override
public Boolean updateDynamicDataSource(GenDsInfo genDsInfo) {
    GenDsInfo _genDsInfo = getById(genDsInfo.getId());
    if (_genDsInfo == null) {
        throw SysException.sysFail("数据不存在");
    }
    // 先移除
    DynamicRoutingDataSource dynamicRoutingDataSource = SpringUtil.getBean(DynamicRoutingDataSource.class);
    dynamicRoutingDataSource.removeDataSource(_genDsInfo.getName());
    // 再添加
    addDynamicDataSource(genDsInfo);
    if (StrUtil.isNotBlank(genDsInfo.getPassword())) {
        _genDsInfo.setPassword(AesUtil.decrypt(genDsInfo.getPassword()));
    }
    return updateById(_genDsInfo);
}
Also used : GenDsInfo(com.xhuicloud.common.datasource.entity.GenDsInfo) DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource)

Example 13 with DynamicRoutingDataSource

use of com.baomidou.dynamic.datasource.DynamicRoutingDataSource in project eden-architect by shiyindaxiaojie.

the class DynamicDataSourceAutoConfiguration method dataSource.

/**
 * 装配数据源
 *
 * @return
 */
@Primary
@Bean
public DataSource dataSource() {
    log.info(AUTOWIRED_DYNAMIC_ROUTING_DATA_SOURCE);
    DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
    dataSource.setPrimary(dynamicDataSourceProperties.getPrimary());
    dataSource.setStrict(dynamicDataSourceProperties.getStrict());
    dataSource.setStrategy(dynamicDataSourceProperties.getStrategy());
    dataSource.setP6spy(dynamicDataSourceProperties.getP6spy());
    dataSource.setSeata(dynamicDataSourceProperties.getSeata());
    return dataSource;
}
Also used : DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Example 14 with DynamicRoutingDataSource

use of com.baomidou.dynamic.datasource.DynamicRoutingDataSource in project dynamic-datasource-samples by dynamic-datasource.

the class MyQuartzAutoConfigurationMode2 method dataSource.

@Primary
@Bean
public DataSource dataSource() {
    DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
    dataSource.setPrimary(properties.getPrimary());
    dataSource.setStrict(properties.getStrict());
    dataSource.setStrategy(properties.getStrategy());
    dataSource.setP6spy(properties.getP6spy());
    dataSource.setSeata(properties.getSeata());
    return dataSource;
}
Also used : DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Example 15 with DynamicRoutingDataSource

use of com.baomidou.dynamic.datasource.DynamicRoutingDataSource in project longmarch by yuyueqty.

the class DataSourceController method remove.

@ApiOperation(value = "删除数据源")
@DeleteMapping
public String remove(String name) {
    DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
    ds.removeDataSource(name);
    return "删除成功";
}
Also used : DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

DynamicRoutingDataSource (com.baomidou.dynamic.datasource.DynamicRoutingDataSource)16 Bean (org.springframework.context.annotation.Bean)8 Primary (org.springframework.context.annotation.Primary)6 DataSource (javax.sql.DataSource)5 DataSourceProperty (com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty)4 ApiOperation (io.swagger.annotations.ApiOperation)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 DbInfo (com.maxqiu.demo.system.entity.DbInfo)1 ServiceException (com.ruoyi.common.exception.ServiceException)1 GenDsInfo (com.xhuicloud.common.datasource.entity.GenDsInfo)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 SQLException (java.sql.SQLException)1 InitializingBean (org.springframework.beans.factory.InitializingBean)1