Search in sources :

Example 1 with DbInfo

use of com.maxqiu.demo.system.entity.DbInfo 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)

Aggregations

DynamicRoutingDataSource (com.baomidou.dynamic.datasource.DynamicRoutingDataSource)1 DataSourceProperty (com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty)1 DbInfo (com.maxqiu.demo.system.entity.DbInfo)1 DataSource (javax.sql.DataSource)1