sharding-jdbc

版本4.0.0-4.1.1

// 配置表规则
TableRuleConfiguration tableRuleConfiguration = new TableRuleConfiguration("table_name","databasename.table_name_${0..3}");

//设置分片策略,这里简单起见直接取模,也可以使用自定义算法来实现分片规则
tableRuleConfiguration.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("applicationId", "table_name_${applicationId % 4}"));
//tableRuleConfiguration.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "id"));
// 配置分片规则
ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration();
shardingRuleConfiguration.getTableRuleConfigs().add(tableRuleConfiguration);
shardingRuleConfiguration.getBindingTableGroups().add("table_name");
// 配置默认分表策略
shardingRuleConfiguration.setDefaultTableShardingStrategyConfig(
new InlineShardingStrategyConfiguration("applicationId", "sale_bill_detail_${applicationId % 4}")
);
// 配置默认分库策略
DataSource datasource = BeanUtil.getDuidDataSource(driverClass,url,user,password);

Properties props=new Properties();
//props.put("sql.show", "true");
Map<String, DataSource> dataSourceMap = new HashMap<>();
dataSourceMap.put("databasename", datasource);
return ShardingDataSourceFactory.createDataSource(dataSourceMap,
shardingRuleConfiguration, props);

 

版本:3.0.0

<!--sharding-jdbc-->
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>3.0.0</version>
</dependency>

TableRuleConfiguration tableRuleConfiguration = new TableRuleConfiguration();
tableRuleConfiguration.setLogicTable("sale_bill_detail");
tableRuleConfiguration.setActualDataNodes("mydatabase_name.sale_bill_detail_${0..3}");
///设置分片策略,这里简单起见直接取模,也可以使用自定义算法来实现分片规则
tableRuleConfiguration.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("applicationId", "table_name_${applicationId % 4}"));
//tableRuleConfiguration.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "id"));
// 配置分片规则
ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration();
shardingRuleConfiguration.getTableRuleConfigs().add(tableRuleConfiguration);
shardingRuleConfiguration.getBindingTableGroups().add("table_name");
// 配置默认分表策略
shardingRuleConfiguration.setDefaultTableShardingStrategyConfig(
new InlineShardingStrategyConfiguration("applicationId", "sale_bill_detail_${applicationId % 4}")
);
// 配置默认分库策略
DataSource datasource = BeanUtil.getDuidDataSource(driverClass,url,user,password);


Properties props=new Properties();
//props.put("sql.show", "true");
Map<String, DataSource> dataSourceMap = new HashMap<>();
dataSourceMap.put("mydatabase_name", datasource);
return ShardingDataSourceFactory.createDataSource(dataSourceMap,
shardingRuleConfiguration, null, props);

posted on 2020-11-19 11:59  andydaopeng  阅读(472)  评论(0编辑  收藏  举报

导航