Search in sources :

Example 1 with DynamicRoutingDataSource

use of com.baomidou.dynamic.datasource.DynamicRoutingDataSource in project jeecg-boot by jeecgboot.

the class DataSourceConfiguration method dataSource.

/**
 * 将动态数据源设置为首选的
 * 当spring存在多个数据源时, 自动注入的是首选的对象
 * 设置为主要的数据源之后,就可以支持shardingjdbc原生的配置方式了
 *
 * @return
 */
@Primary
@Bean
public DataSource dataSource(DynamicDataSourceProvider dynamicDataSourceProvider) {
    DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
    dataSource.setPrimary(dynamicDataSourceProperties.getPrimary());
    dataSource.setStrict(dynamicDataSourceProperties.getStrict());
    dataSource.setStrategy(dynamicDataSourceProperties.getStrategy());
    dataSource.setProvider(dynamicDataSourceProvider);
    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 2 with DynamicRoutingDataSource

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

use of com.baomidou.dynamic.datasource.DynamicRoutingDataSource in project dynamic-datasource-spring-boot-starter by baomidou.

the class DynamicDataSourceAutoConfiguration method dataSource.

@Bean
@ConditionalOnMissingBean
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) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) InitializingBean(org.springframework.beans.factory.InitializingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with DynamicRoutingDataSource

use of com.baomidou.dynamic.datasource.DynamicRoutingDataSource in project springboot by ipcrystal.

the class DataSourceConfiguration method dataSource.

/**
 * 将动态数据源设置为首选的
 * 当spring存在多个数据源时, 自动注入的是首选的对象
 * 设置为主要的数据源之后,就可以支持shardingjdbc原生的配置方式了
 *
 * @return DataSource
 */
@Primary
@Bean
public DataSource dataSource() {
    DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
    dataSource.setPrimary(dynamicDataSourceProperties.getPrimary());
    dataSource.setStrict(dynamicDataSourceProperties.getStrict());
    dataSource.setStrategy(dynamicDataSourceProperties.getStrategy());
    dataSource.setP6spy(dynamicDataSourceProperties.getP6spy());
    dataSource.setSeata(dynamicDataSourceProperties.getSeata());
    dataSource.addDataSource(SHARDING_DATA_SOURCE_NAME, shardingDataSource);
    return dataSource;
}
Also used : DynamicRoutingDataSource(com.baomidou.dynamic.datasource.DynamicRoutingDataSource) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Example 5 with DynamicRoutingDataSource

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

the class DataSourceController method remove.

/**
 * 删除数据源
 */
@DeleteMapping
public String remove(String name) {
    DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
    ds.removeDataSource(name);
    return "删除成功";
}
Also used : 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