Use Java API

Import Maven Dependency

  1. <dependency>
  2. <groupId>org.apache.shardingsphere</groupId>
  3. <artifactId>shardingsphere-jdbc-core</artifactId>
  4. <version>${shardingsphere.version}</version>
  5. </dependency>

Configure Rule

ShardingSphere-JDBC Java API consists of data sources, rules and properties configuration. The following example is the configuration of 2 databases and 2 tables, whose databases take module and split according to order_id, tables take module and split according to order_id .

Note: The example database connection pool is HikariCP, which can be replaced with other mainstream database connection pools according to business scenarios.

  1. // Configure actual data sources
  2. Map<String, DataSource> dataSourceMap = new HashMap<>();
  3. // Configure the first data source
  4. HikariDataSource dataSource1 = new HikariDataSource();
  5. dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
  6. dataSource1.setJdbcUrl("jdbc:mysql://localhost:3306/ds0");
  7. dataSource1.setUsername("root");
  8. dataSource1.setPassword("");
  9. dataSourceMap.put("ds0", dataSource1);
  10. // Configure the second data source
  11. HikariDataSource dataSource2 = new HikariDataSource();
  12. dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
  13. dataSource2.setJdbcUrl("jdbc:mysql://localhost:3306/ds1");
  14. dataSource2.setUsername("root");
  15. dataSource2.setPassword("");
  16. dataSourceMap.put("ds1", dataSource2);
  17. // Configure order table rule
  18. ShardingTableRuleConfiguration orderTableRuleConfig = new ShardingTableRuleConfiguration("t_order", "ds${0..1}.t_order${0..1}");
  19. // Configure database sharding strategy
  20. orderTableRuleConfig.setDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "dbShardingAlgorithm"));
  21. // Configure table sharding strategy
  22. orderTableRuleConfig.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "tableShardingAlgorithm"));
  23. // Omit t_order_item table rule configuration ...
  24. // ...
  25. // Configure sharding rule
  26. ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
  27. shardingRuleConfig.getTables().add(orderTableRuleConfig);
  28. // Configure database sharding algorithm
  29. Properties dbShardingAlgorithmrProps = new Properties();
  30. dbShardingAlgorithmrProps.setProperty("algorithm-expression", "ds${user_id % 2}");
  31. shardingRuleConfig.getShardingAlgorithms().put("dbShardingAlgorithm", new ShardingSphereAlgorithmConfiguration("INLINE", dbShardingAlgorithmrProps));
  32. // Configure table sharding algorithm
  33. Properties tableShardingAlgorithmrProps = new Properties();
  34. tableShardingAlgorithmrProps.setProperty("algorithm-expression", "t_order${order_id % 2}");
  35. shardingRuleConfig.getShardingAlgorithms().put("tableShardingAlgorithm", new ShardingSphereAlgorithmConfiguration("INLINE", tableShardingAlgorithmrProps));
  36. // Create ShardingSphereDataSource
  37. DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, Collections.singleton(shardingRuleConfig), new Properties());

Use ShardingSphereDataSource

The ShardingSphereDataSource created by ShardingSphereDataSourceFactory implements the standard JDBC DataSource interface. Developer can choose to use native JDBC or ORM frameworks such as JPA or MyBatis through the DataSource.

Take native JDBC usage as an example:

  1. DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, Collections.singleton(shardingRuleConfig), new Properties());
  2. String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";
  3. try (
  4. Connection conn = dataSource.getConnection();
  5. PreparedStatement ps = conn.prepareStatement(sql)) {
  6. ps.setInt(1, 10);
  7. ps.setInt(2, 1000);
  8. try (ResultSet rs = ps.executeQuery()) {
  9. while(rs.next()) {
  10. // ...
  11. }
  12. }
  13. }