win7、JDK1.7、IntelliJ IDEA 2017.2.5。
File-Open...,选择sharding-jdbc-1.5.4.1下的sharding-jdbc-example,先从sharding-jdbc-example-mybatis项目开始,如图01。
修改shardingContext.xml里访问MySQL的用户名和密码,我的密码是:123456。
public final class SingleKeyModuloDatabaseShardingAlgorithm implements SingleKeyDatabaseShardingAlgorithm {
@Override
public String doEqualSharding(final Collection availableTargetNames, final ShardingValue shardingValue) {
System.out.println("###### 01 doEqualSharding(),availableTargetNames=" + availableTargetNames + ",shardingValue=" + shardingValue);
for (String each : availableTargetNames) {
if (each.endsWith(shardingValue.getValue() % 2 + "")) {
System.out.println("###### 01 doEqualSharding(),命中=" + each);
return each;
}
}
throw new UnsupportedOperationException();
}
@Override
public Collection doInSharding(final Collection availableTargetNames, final ShardingValue shardingValue) {
System.out.println("###### 01 doInSharding(),availableTargetNames=" + availableTargetNames + ",shardingValue=" + shardingValue);
Collection result = new LinkedHashSet<>(availableTargetNames.size());
Collection values = shardingValue.getValues();
for (Integer value : values) {
for (String each : availableTargetNames) {
if (each.endsWith(value % 2 + "")) {
result.add(each);
}
}
}
System.out.println("###### 01 doInSharding(),命中=" + result);
return result;
}
@Override
public Collection doBetweenSharding(final Collection availableTargetNames, final ShardingValue shardingValue) {
System.out.println("###### 01 doBetweenSharding(),availableTargetNames=" + availableTargetNames + ",shardingValue=" + shardingValue);
Collection result = new LinkedHashSet<>(availableTargetNames.size());
Range range = shardingValue.getValueRange();
for (Integer value = range.lowerEndpoint(); value <= range.upperEndpoint(); value++) {
for (String each : availableTargetNames) {
if (each.endsWith(value % 2 + "")) {
result.add(each);
}
}
}
System.out.println("###### 01 doBetweenSharding(),命中=" + result);
return result;
}
}
public final class SingleKeyModuloTableShardingAlgorithm implements SingleKeyTableShardingAlgorithm {
@Override
public String doEqualSharding(final Collection availableTargetNames, final ShardingValue shardingValue) {
System.out.println("###### 02 doEqualSharding(),availableTargetNames=" + availableTargetNames + ",shardingValue=" + shardingValue);
for (String each : availableTargetNames) {
if (each.endsWith(shardingValue.getValue() % 2 + "")) {
System.out.println("###### 02 doEqualSharding(),命中=" + each);
return each;
}
}
throw new UnsupportedOperationException();
}
@Override
public Collection doInSharding(final Collection availableTargetNames, final ShardingValue shardingValue) {
System.out.println("###### 02 doInSharding(),availableTargetNames=" + availableTargetNames + ",shardingValue=" + shardingValue);
Collection result = new LinkedHashSet<>(availableTargetNames.size());
Collection values = shardingValue.getValues();
for (Long value : values) {
for (String each : availableTargetNames) {
if (each.endsWith(value % 2 + "")) {
result.add(each);
}
}
}
System.out.println("###### 02 doInSharding(),命中=" + result);
return result;
}
@Override
public Collection doBetweenSharding(final Collection availableTargetNames, final ShardingValue shardingValue) {
System.out.println("###### 02 doBetweenSharding(),availableTargetNames=" + availableTargetNames + ",shardingValue=" + shardingValue);
Collection result = new LinkedHashSet<>(availableTargetNames.size());
Range range = shardingValue.getValueRange();
for (Long i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) {
for (String each : availableTargetNames) {
if (each.endsWith(i % 2 + "")) {
result.add(each);
}
}
}
System.out.println("###### 02 doBetweenSharding(),命中=" + result);
return result;
}
}
分析Dao操作与对应的日志输出。
orderService.clear();不涉及上述两个算法。
orderService.fooService();
###### 01 doEqualSharding(),availableTargetNames=[ds_0, ds_1],shardingValue=ShardingValue(logicTableName=t_order, columnName=user_id, value=10, values=[], valueRange=null)
###### 01 doEqualSharding(),命中=ds_0
###### 02 doEqualSharding(),availableTargetNames=[t_order_1, t_order_0],shardingValue=ShardingValue(logicTableName=t_order, columnName=order_id, value=149948054085042176, values=[], valueRange=null)
###### 02 doEqualSharding(),命中=t_order_0
Generated key1 of order_id:149948054085042176
###### 01 doEqualSharding(),availableTargetNames=[ds_0, ds_1],shardingValue=ShardingValue(logicTableName=t_order, columnName=user_id, value=11, values=[], valueRange=null)
###### 01 doEqualSharding(),命中=ds_1
###### 02 doEqualSharding(),availableTargetNames=[t_order_1, t_order_0],shardingValue=ShardingValue(logicTableName=t_order, columnName=order_id, value=149948054353477632, values=[], valueRange=null)
###### 02 doEqualSharding(),命中=t_order_0
Generated key2 of order_id:149948054353477632
###### 01 doInSharding(),availableTargetNames=[ds_0, ds_1],shardingValue=ShardingValue(logicTableName=t_order, columnName=user_id, value=null, values=[10, 11], valueRange=null)
###### 01 doInSharding(),命中=[ds_0, ds_1]
逻辑表,是相对于物理表而言的。因为Sharding-JDBC的存在,站在MyBatis映射文件的角度来说,它根本不知道有ds_0、ds_1、t_order_0、t_order_1等的存在,只知道t_order,仿佛在操作单数据源似的。