Search in sources :

Example 1 with AbstractJdbcDataSourceProvider

use of com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider 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)

Example 2 with AbstractJdbcDataSourceProvider

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

the class JdbcDataSourceConfig method jdbcDynamicDataSourceProvider.

@Bean
public DynamicDataSourceProvider jdbcDynamicDataSourceProvider() {
    return new AbstractJdbcDataSourceProvider(driver, url, username, password) {

        @Override
        protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
            ResultSet rs = statement.executeQuery("SELECT * FROM " + DATA_SOURCE_INFO);
            Map<String, DataSourceProperty> map = new HashMap<>(8);
            while (rs.next()) {
                String name = rs.getString("data_source_name");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String url = rs.getString("url");
                String driver = rs.getString("driver_class_name");
                if (StrUtil.isBlank(name) || StrUtil.isBlank(username) || StrUtil.isBlank(password) || StrUtil.isBlank(url) || StrUtil.isBlank(driver)) {
                    continue;
                }
                DataSourceProperty property = new DataSourceProperty();
                property.setPoolName(name);
                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

AbstractJdbcDataSourceProvider (com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider)2 DataSourceProperty (com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 HashMap (java.util.HashMap)2 Bean (org.springframework.context.annotation.Bean)2