Search in sources :

Example 16 with DataSourceProperty

use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project jeecg-boot by jeecgboot.

the class CommonUtils method getDataSourceProperty.

/**
 * 根据数据源key获取DataSourceProperty
 * @param sourceKey
 * @return
 */
public static DataSourceProperty getDataSourceProperty(String sourceKey) {
    DynamicDataSourceProperties prop = SpringContextUtils.getApplicationContext().getBean(DynamicDataSourceProperties.class);
    Map<String, DataSourceProperty> map = prop.getDatasource();
    DataSourceProperty db = (DataSourceProperty) map.get(sourceKey);
    return db;
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) DynamicDataSourceProperties(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties)

Example 17 with DataSourceProperty

use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project jeecg-boot by jeecgboot.

the class CommonUtils method getDataSourceConnect.

/**
 * 根据sourceKey 获取数据源连接
 * @param sourceKey
 * @return
 * @throws SQLException
 */
public static Connection getDataSourceConnect(String sourceKey) throws SQLException {
    if (oConvertUtils.isEmpty(sourceKey)) {
        sourceKey = "master";
    }
    DynamicDataSourceProperties prop = SpringContextUtils.getApplicationContext().getBean(DynamicDataSourceProperties.class);
    Map<String, DataSourceProperty> map = prop.getDatasource();
    DataSourceProperty db = (DataSourceProperty) map.get(sourceKey);
    if (db == null) {
        return null;
    }
    DriverManagerDataSource ds = new DriverManagerDataSource();
    ds.setDriverClassName(db.getDriverClassName());
    ds.setUrl(db.getUrl());
    ds.setUsername(db.getUsername());
    ds.setPassword(db.getPassword());
    return ds.getConnection();
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) DynamicDataSourceProperties(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties)

Example 18 with DataSourceProperty

use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty 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 19 with DataSourceProperty

use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project springboot-templet-start by thedestiny.

the class DataSourceController method fillSourceProperty.

private DataSourceProperty fillSourceProperty(DataSourceDTO dto) {
    DataSourceProperty dataSourceProperty = new DataSourceProperty();
    BeanUtils.copyProperties(dto, dataSourceProperty);
    return dataSourceProperty;
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty)

Example 20 with DataSourceProperty

use of com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty in project dynamic-datasource-samples by dynamic-datasource.

the class LoadDsApplication method dynamicDataSourceProvider.

@Bean
public DynamicDataSourceProvider dynamicDataSourceProvider() {
    return new AbstractJdbcDataSourceProvider("org.h2.Driver", "jdbc:h2:mem:test", "sa", "") {

        @Override
        protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
            // *************** 实际运行应直接从库中查出,演示所以需要先插入数据***************
            statement.execute("CREATE TABLE IF NOT EXISTS `DB`\n" + "(\n" + "    `name`   VARCHAR(30) NULL DEFAULT NULL,\n" + "    `username`   VARCHAR(30) NULL DEFAULT NULL,\n" + "    `password`   VARCHAR(30) NULL DEFAULT NULL,\n" + "    `url`   VARCHAR(30) NULL DEFAULT NULL,\n" + "    `driver`   VARCHAR(30) NULL DEFAULT NULL\n" + ")");
            statement.executeUpdate("insert into DB values ('db1','sa','','jdbc:h2:mem:test2','org.h2.Driver')");
            statement.executeUpdate("insert into DB values ('db2','sa','','jdbc:h2:mem:test3','org.h2.Driver')");
            statement.executeUpdate("insert into DB values ('db3','sa','','jdbc:h2:mem:test4','org.h2.Driver')");
            Map<String, DataSourceProperty> map = new HashMap<>();
            // *************** ↑↑↑↑↑↑↑ END  ***************
            ResultSet rs = statement.executeQuery("select * from DB");
            while (rs.next()) {
                String name = rs.getString("name");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String url = rs.getString("url");
                String driver = rs.getString("driver");
                DataSourceProperty property = new DataSourceProperty();
                property.setUsername(username);
                property.setPassword(password);
                property.setUrl(url);
                property.setDriverClassName(driver);
                map.put(name, property);
            }
            return map;
        }
    };
}
Also used : DataSourceProperty(com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty) HashMap(java.util.HashMap) Statement(java.sql.Statement) AbstractJdbcDataSourceProvider(com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider) ResultSet(java.sql.ResultSet) Bean(org.springframework.context.annotation.Bean)

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