commit | 3bab810865364231e67eec084dcc16c2280a7c41 | [log] [tgz] |
---|---|---|
author | terrymanu <terrymanu@163.com> | Fri Aug 11 23:21:46 2017 +0800 |
committer | terrymanu <terrymanu@163.com> | Fri Aug 11 23:21:46 2017 +0800 |
tree | deea0aa3c23726acb484bbf650f5add52d6abbe2 | |
parent | 88635fda03e85dfb58982627a5b25e72daf6d165 [diff] |
upgrade 1.5.1
Sharding-JDBC is a JDBC extension, provides distributed features such as sharding, read/write splitting and BASE transaction.
=
, BETWEEN
and IN
supported.<!-- import sharding-jdbc core --> <dependency> <groupId>com.dangdang</groupId> <artifactId>sharding-jdbc-core</artifactId> <version>${latest.release.version}</version> </dependency> <!-- import other module if need -->
ShardingRule shardingRule = ShardingRule.builder() .dataSourceRule(dataSourceRule) .tableRules(tableRuleList) .databaseShardingStrategy(new DatabaseShardingStrategy("sharding_column", new XXXShardingAlgorithm())) .tableShardingStrategy(new TableShardingStrategy("sharding_column", new XXXShardingAlgorithm()))) .build();
DataSource dataSource = ShardingDataSourceFactory.createDataSource(shardingRule); 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=?"; try ( Connection conn = dataSource.getConnection(); PreparedStatement preparedStatement = conn.prepareStatement(sql)) { preparedStatement.setInt(1, 10); preparedStatement.setInt(2, 1001); try (ResultSet rs = preparedStatement.executeQuery()) { while(rs.next()) { System.out.println(rs.getInt(1)); System.out.println(rs.getInt(2)); } } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:rdb="http://www.dangdang.com/schema/ddframe/rdb" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.dangdang.com/schema/ddframe/rdb http://www.dangdang.com/schema/ddframe/rdb/rdb.xsd "> <context:property-placeholder location="classpath:conf/rdb/conf.properties" ignore-unresolvable="true"/> <bean id="dbtbl_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_0"/> <property name="username" value="root"/> <property name="password" value=""/> </bean> <bean id="dbtbl_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_1"/> <property name="username" value="root"/> <property name="password" value=""/> </bean> <rdb:strategy id="orderTableStrategy" sharding-columns="order_id" algorithm-expression="t_order_${order_id.longValue() % 4}"/> <rdb:strategy id="orderItemTableStrategy" sharding-columns="order_id" algorithm-expression="t_order_item_${order_id.longValue() % 4}"/> <rdb:data-source id="shardingDataSource"> <rdb:sharding-rule data-sources="dbtbl_0,dbtbl_1"> <rdb:table-rules> <rdb:table-rule logic-table="t_order" actual-tables="t_order_${0..3}" table-strategy="orderTableStrategy"/> <rdb:table-rule logic-table="t_order_item" actual-tables="t_order_item_${0..3}" table-strategy="orderItemTableStrategy"/> </rdb:table-rules> <rdb:default-database-strategy sharding-columns="none" algorithm-class="com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm"/> </rdb:sharding-rule> </rdb:data-source> </beans>