Java Code Examples for io.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory

The following examples show how to use io.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Bean(name = "myBatisDataSource")
    @Primary
    @ConfigurationProperties(prefix = "data-source.single-db")
    public DataSource userDataSource() throws IOException, SQLException {
        if(!useMasterSlaveDataSource) {
            logger.info("-------------------- singleDB DataSource init ---------------------");
            return DataSourceBuilder.create().build();
        }else {
            logger.info("-------------------- masterSlave DataSource init ---------------------");
//            DataSource dataSource = YamlMasterSlaveDataSourceFactory.createDataSource(new File(
//                    DataSourceConfiguration.class.getResource("/META-INF/yamlMasterSlave.yaml").getFile()));
//            return dataSource;
            return null == masterSlaveProperties.getMasterDataSourceName()
                    ? ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingProperties.getShardingRuleConfiguration(), configMapProperties.getConfigMap(), propMapProperties.getProps())
                    : MasterSlaveDataSourceFactory.createDataSource(
                    dataSourceMap, masterSlaveProperties.getMasterSlaveRuleConfiguration(), configMapProperties.getConfigMap(), propMapProperties.getProps());
        }
    }
 
Example 2
Source Project: spring-boot-demo   Source File: DataSourceShardingConfig.java    License: MIT License 6 votes vote down vote up
@Bean(name = "dataSource")
@Primary
public DataSource dataSource() throws SQLException {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    // 设置分库策略
    shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds${user_id % 2}"));
    // 设置规则适配的表
    shardingRuleConfig.getBindingTableGroups().add("t_order");
    // 设置分表策略
    shardingRuleConfig.getTableRuleConfigs().add(orderTableRule());
    shardingRuleConfig.setDefaultDataSourceName("ds0");
    shardingRuleConfig.setDefaultTableShardingStrategyConfig(new NoneShardingStrategyConfiguration());

    Properties properties = new Properties();
    properties.setProperty("sql.show", "true");

    return ShardingDataSourceFactory.createDataSource(dataSourceMap(), shardingRuleConfig, new ConcurrentHashMap<>(16), properties);
}
 
Example 3
@Override
public DataSource createDataSource() throws SQLException {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
    shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
    shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
    shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "demo_ds_${user_id % 2}"));
    shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm()));
    Properties properties = new Properties();
    properties.setProperty("max.connections.size.per.query", "16");
    dataSource = ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, new HashMap<String, Object>(), properties);
    return dataSource;
}