快速入门

使用API配置

Maven安装

  1. <!-- 引入sharding-jdbc核心模块 -->
  2. <dependency>
  3. <groupId>com.dangdang</groupId>
  4. <artifactId>sharding-jdbc-core</artifactId>
  5. <version>${sharding-jdbc.version}</version>
  6. </dependency>

规则配置

Sharding-JDBC的分库分表通过规则配置描述,请简单浏览配置全貌:

  1. ShardingRule shardingRule = ShardingRule.builder()
  2. .dataSourceRule(dataSourceRule)
  3. .tableRules(tableRuleList)
  4. .databaseShardingStrategy(new DatabaseShardingStrategy("sharding_column", new XXXShardingAlgorithm()))
  5. .tableShardingStrategy(new TableShardingStrategy("sharding_column", new XXXShardingAlgorithm())))
  6. .build();

规则配置包括数据源配置、表规则配置、分库策略和分表策略组成。这只是最简单的配置方式,实际使用可更加灵活,如:多分片键,分片策略直接和表规则配置绑定等。

详细的规则配置请参考分库分表

使用基于ShardingDataSource的JDBC接口

通过ShardingDataSourceFactory工厂和规则配置对象获取ShardingDataSource,ShardingDataSource实现自JDBC的标准接口DataSource。然后可通过DataSource选择使用原生JDBC开发,或者使用JPA, MyBatis等ORM工具。以JDBC原生实现为例:

  1. DataSource dataSource = ShardingDataSourceFactory.createDataSource(shardingRule);
  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 preparedStatement = conn.prepareStatement(sql)) {
  6. preparedStatement.setInt(1, 10);
  7. preparedStatement.setInt(2, 1001);
  8. try (ResultSet rs = preparedStatement.executeQuery()) {
  9. while(rs.next()) {
  10. System.out.println(rs.getInt(1));
  11. System.out.println(rs.getInt(2));
  12. }
  13. }
  14. }

使用Spring

Maven安装

  1. <!-- 引入sharding-jdbc核心模块 -->
  2. <dependency>
  3. <groupId>com.dangdang</groupId>
  4. <artifactId>sharding-jdbc-config-spring</artifactId>
  5. <version>${sharding-jdbc.version}</version>
  6. </dependency>

Spring命名空间

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:rdb="http://www.dangdang.com/schema/ddframe/rdb"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.dangdang.com/schema/ddframe/rdb
  11. http://www.dangdang.com/schema/ddframe/rdb/rdb.xsd
  12. ">
  13. <context:property-placeholder location="classpath:conf/rdb/conf.properties" ignore-unresolvable="true"/>
  14. <bean id="dbtbl_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  15. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  16. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_0"/>
  17. <property name="username" value="root"/>
  18. <property name="password" value=""/>
  19. </bean>
  20. <bean id="dbtbl_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  21. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  22. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_1"/>
  23. <property name="username" value="root"/>
  24. <property name="password" value=""/>
  25. </bean>
  26. <rdb:strategy id="orderTableStrategy" sharding-columns="order_id" algorithm-expression="t_order_${order_id.longValue() % 4}"/>
  27. <rdb:strategy id="orderItemTableStrategy" sharding-columns="order_id" algorithm-expression="t_order_item_${order_id.longValue() % 4}"/>
  28. <rdb:data-source id="shardingDataSource">
  29. <rdb:sharding-rule data-sources="dbtbl_0,dbtbl_1">
  30. <rdb:table-rules>
  31. <rdb:table-rule logic-table="t_order" actual-tables="t_order_${0..3}" table-strategy="orderTableStrategy"/>
  32. <rdb:table-rule logic-table="t_order_item" actual-tables="t_order_item_${0..3}" table-strategy="orderItemTableStrategy"/>
  33. </rdb:table-rules>
  34. <rdb:default-database-strategy sharding-columns="none" algorithm-class="com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm"/>
  35. </rdb:sharding-rule>
  36. </rdb:data-source>
  37. </beans>

详细的规则配置请参考配置手册