分库:/*** 这里使用的都是单键分片策略
* 示例分库策略是:
* id<=20使用acnew库
* 其余使用acold库*/
packagecom..config;importcom.dangdang.ddframe.rdb.sharding.api.ShardingValue;importcom.dangdang.ddframe.rdb.sharding.api.strategy.database.SingleKeyDatabaseShardingAlgorithm;importcom.google.common.collect.Range;importcom..database.Ac_newConfig;importcom..database.Ac_oldConfig;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;importjava.util.Collection;importjava.util.LinkedHashSet;
@Componentpublic class FkShardingAlgorithm implements SingleKeyDatabaseShardingAlgorithm{
@AutowiredprivateAc_newConfig ac_newConfig;
@AutowiredprivateAc_oldConfig ac_OldConfig;
@Overridepublic String doEqualSharding(final Collection availableTargetNames,final ShardingValueshardingValue) {
Long value=shardingValue.getValue();if (value <= 20L) {returnac_newConfig.getDatabaseName();
}else{returnac_OldConfig.getDatabaseName();
}
}
@Overridepublic Collection doInSharding(final Collection availableTargetNames,final ShardingValueshardingValue) {
Collection result = new LinkedHashSet<>(availableTargetNames.size());for(Long value : shardingValue.getValues()) {if (value <= 20L) {
result.add(ac_newConfig.getDatabaseName());
}else{
result.add(ac_OldConfig.getDatabaseName());
}
}returnresult;
}
@Overridepublic Collection doBetweenSharding(final CollectionavailableTargetNames,final ShardingValueshardingValue) {
Collection result = new LinkedHashSet<>(availableTargetNames.size());
Range range =shardingValue.getValueRange();for (Long value = range.lowerEndpoint(); value <= range.upperEndpoint(); value++) {if (value <= 20L) {
result.add(ac_newConfig.getDatabaseName());
}else{
result.add(ac_OldConfig.getDatabaseName());
}
}returnresult;
}
}
分表:/*** 这里使用的都是单键分片策略
* 示例分表策略是:
* type为奇数使用one表
* type为偶数使用two表*/
packagecom..config;importcom.dangdang.ddframe.rdb.sharding.api.ShardingValue;importcom.dangdang.ddframe.rdb.sharding.api.strategy.database.SingleKeyDatabaseShardingAlgorithm;importcom.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm;importcom.google.common.collect.Range;importorg.springframework.stereotype.Component;importjava.util.Collection;importjava.util.LinkedHashSet;
@Componentpublic class FbShardingAlgorithm implements SingleKeyTableShardingAlgorithm{
@Overridepublic String doEqualSharding(final Collection tableNames,final ShardingValueshardingValue) {for(String table : tableNames){
System.out.println(table+"---"+shardingValue);
System.out.println(shardingValue.getValue()%2+"");if("0".endsWith(shardingValue.getValue()%2+"")){return "one";
}else{return "two";
}
}throw new IllegalArgumentException("无分表参数 无法定位具体数据表");
}
@Overridepublic Collection doInSharding(final Collection tableNames,final ShardingValueshardingValue) {
Collection result = new LinkedHashSet<>(tableNames.size());for(Long value : shardingValue.getValues()) {for(String tableName : tableNames) {if(tableName.endsWith(String.valueOf(value))) {
result.add(tableName);
}
}
}returnresult;
}
@Overridepublic Collection doBetweenSharding(final Collection tableNames,final ShardingValueshardingValue) {
Collection result = new LinkedHashSet<>(tableNames.size());
Range range =shardingValue.getValueRange();for (Long i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) {for(String each : tableNames) {if (each.endsWith(i % 2 + "")) {
result.add(each);
}
}
}returnresult;
}
}
实现类:packagecom..config;importcom.dangdang.ddframe.rdb.sharding.api.ShardingDataSourceFactory;importcom.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;importcom.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;importcom.dangdang.ddframe.rdb.sharding.api.rule.TableRule;importcom.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;importcom.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;importcom.dangdang.ddframe.rdb.sharding.keygen.DefaultKeyGenerator;importcom.dangdang.ddframe.rdb.sharding.keygen.KeyGenerator;importcom..database.Ac_newConfig;importcom..database.Ac_oldConfig;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importjavax.sql.DataSource;importjava.sql.SQLException;importjava.util.Arrays;importjava.util.HashMap;importjava.util.Map;
@Configurationpublic classFkFbShardingAlgorithm {
@AutowiredprivateAc_newConfig ac_newConfig;
@AutowiredprivateAc_oldConfig ac_OldConfig;
@AutowiredprivateFbShardingAlgorithm fbShardingAlgorithm;
@AutowiredprivateFkShardingAlgorithm fkShardingAlgorithm;
@Beanpublic DataSource getDataSource() throwsSQLException {returnbuildDataSource();
}private DataSource buildDataSource() throwsSQLException {//分库设置
Map dataSourcemap = new HashMap<>(2);//添加两个数据库 Ac_new 和 Ac_old
dataSourcemap.put(ac_newConfig.getDatabaseName(),ac_newConfig.createDataSource());
dataSourcemap.put(ac_OldConfig.getDatabaseName(),ac_OldConfig.createDataSource());//设置默认数据库(Ac_new)
DataSourceRule dataSourceRule = newDataSourceRule(dataSourcemap,ac_newConfig.getDatabaseName());//分表设置,大致思想就是将查询虚拟表Save根据一定规则映射到真实表中去
TableRule orderTableRule = TableRule.builder("save")
.actualTables(Arrays.asList("one", "two"))
.dataSourceRule(dataSourceRule)
.build();//分库分表策略
ShardingRule shardingRule =ShardingRule.builder()
.dataSourceRule(dataSourceRule)
.tableRules(Arrays.asList(orderTableRule))
.databaseShardingStrategy(new DatabaseShardingStrategy("id", fkShardingAlgorithm))
.tableShardingStrategy(new TableShardingStrategy("type",fbShardingAlgorithm)).build();
DataSource dataSource=ShardingDataSourceFactory.createDataSource(shardingRule);returndataSource;
}
@BeanpublicKeyGenerator keyGenerator() {return newDefaultKeyGenerator();
}
}