概要:
-
如果按照application.properties的配置,只能根据某个列进行区分规则,那么可以通过配置类来达到更精确的目的
- 其他代码 参考:https://www.cnblogs.com/yi1036943655/p/15700170.html
application.properties
# 精确分表策略 spring.shardingsphere.sharding.tables.s_user.table-strategy.standard.sharding-column=age spring.shardingsphere.sharding.tables.s_user.table-strategy.standard.precise-algorithm-class-name=com.sharding.demo.config.TableShardingAlgorithm
TableShardingAlgorithm
package com.sharding.demo.config; import lombok.extern.slf4j.Slf4j; import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm; import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue; import java.util.Collection; @Slf4j public class TableShardingAlgorithm implements PreciseShardingAlgorithm<String> { @Override public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> preciseShardingValue) { try{ log.info("preciseShardingValue:{},column:{}",preciseShardingValue.getValue(),preciseShardingValue.getColumnName()); for (String each : availableTargetNames) { log.info("each:" + each); } // 返回 就是最后落实的表 return "s_user0"; } catch (Exception e){ e.printStackTrace(); } throw new IllegalArgumentException(); } }