Search in sources :

Example 6 with DynamicRoutingDataSource

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

the class DataSourceController method add.

/**
 * 添加数据源
 */
@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)

Example 7 with DynamicRoutingDataSource

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

the class MyDataSourceConfiguration method dataSource.

/**
 * 将动态数据源设置为首选的
 * 当spring存在多个数据源时, 自动注入的是首选的对象
 * 设置为主要的数据源之后,就可以支持shardingJdbc原生的配置方式了
 */
@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 8 with DynamicRoutingDataSource

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

the class MyDataSourceConfiguration 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 9 with DynamicRoutingDataSource

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

the class DynamicRoutingDataSourceAutoConfiguration method dataSource.

/**
 * 核心动态数据源组件
 *
 * @param properties 动态数据源配置属性
 * @return
 */
@Bean
@ConditionalOnMissingBean
public DynamicRoutingDataSource dataSource(final DynamicDataSourceProperties properties) {
    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) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 10 with DynamicRoutingDataSource

use of com.baomidou.dynamic.datasource.DynamicRoutingDataSource in project RuoYi-Flowable-Plus by KonBAI-Q.

the class DataBaseHelper method getDataBaseType.

/**
 * 获取当前数据库类型
 */
public static DataBaseType getDataBaseType() {
    DynamicRoutingDataSource ds = (DynamicRoutingDataSource) SpringUtils.getBean(DataSource.class);
    DataSource dataSource = ds.determineDataSource();
    try (Connection conn = dataSource.getConnection()) {
        DatabaseMetaData metaData = conn.getMetaData();
        String databaseProductName = metaData.getDatabaseProductName();
        return DataBaseType.find(databaseProductName);
    } catch (SQLException e) {
        throw new ServiceException(e.getMessage());
    }
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) SQLException(java.sql.SQLException) DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) DataSource(javax.sql.DataSource) DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource)

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