use of io.shardingjdbc.core.fixture.PreciseOrderShardingAlgorithm in project sharding-jdbc by shardingjdbc.
the class AbstractShardingJDBCDatabaseAndTableTest method initShardingDataSources.
@Before
public void initShardingDataSources() throws SQLException {
if (!getShardingDataSources().isEmpty()) {
return;
}
Map<DatabaseType, Map<String, DataSource>> dataSourceMap = createDataSourceMap();
for (Map.Entry<DatabaseType, Map<String, DataSource>> entry : dataSourceMap.entrySet()) {
final ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
orderTableRuleConfig.setLogicTable("t_order");
List<String> orderActualDataNodes = new LinkedList<>();
for (String dataSourceName : entry.getValue().keySet()) {
orderActualDataNodes.add(dataSourceName + ".t_order_${0..1}");
}
orderTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderActualDataNodes));
shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration();
orderItemTableRuleConfig.setLogicTable("t_order_item");
List<String> orderItemActualDataNodes = new LinkedList<>();
for (String dataSourceName : entry.getValue().keySet()) {
orderItemActualDataNodes.add(dataSourceName + ".t_order_item_${0..1}");
}
orderItemTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderItemActualDataNodes));
orderItemTableRuleConfig.setKeyGeneratorColumnName("item_id");
orderItemTableRuleConfig.setKeyGenerator(new IncrementKeyGenerator());
shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig);
TableRuleConfiguration configTableRuleConfig = new TableRuleConfiguration();
configTableRuleConfig.setLogicTable("t_config");
shardingRuleConfig.getTableRuleConfigs().add(configTableRuleConfig);
shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseOrderShardingAlgorithm(), new RangeOrderShardingAlgorithm()));
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("user_id", new PreciseOrderShardingAlgorithm(), new RangeOrderShardingAlgorithm()));
ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, entry.getValue().keySet());
getShardingDataSources().put(entry.getKey(), new ShardingDataSource(entry.getValue(), shardingRule));
}
}
Aggregations