kidgrow-commons/kidgrow-db-spring-boot-starter/src/main/java/com/kidgrow/db/sharding/HashModTableSuffix.java
@@ -13,17 +13,21 @@ public class HashModTableSuffix implements TableSuffix { private String hash; private static final int MOD = 100; private Long tableSize = 64L; private String tableSuffix="-000"; public HashModTableSuffix(String hash) { public HashModTableSuffix(String hash,long tableSize, String tableSuffix) { if (StringUtils.isEmpty(hash)) { throw new IllegalArgumentException("hash should not be null or empty"); } this.hash = hash; this.tableSize=tableSize; this.tableSuffix=tableSuffix; } @Override public String getSuffix() { return String.format("_%d", hash.hashCode() / MOD); String format = "%0" + (tableSuffix.length() - 1) + "d"; return "_"+String.format(format, Math.abs(hash.hashCode()) % tableSize); } } kidgrow-commons/kidgrow-db-spring-boot-starter/src/main/java/com/kidgrow/db/sharding/TableSuffix.java
@@ -12,8 +12,8 @@ */ public interface TableSuffix { static TableSuffix ofHash(String hash) { return new HashModTableSuffix(hash); static TableSuffix ofHash(String hash,long tableSize, String tableSuffix) { return new HashModTableSuffix(hash,tableSize,tableSuffix); } static TableSuffix ofMonth(Date date) { kidgrow-demo/kidgrow-demo-order/src/test/java/com/kidgrow/order/mapper/OrderInfoMapperTest.java
@@ -2,6 +2,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.kidgrow.common.context.DynamicTableContextHolder; import com.kidgrow.db.sharding.TableSuffix; import com.kidgrow.order.entity.OrderInfo; import com.kidgrow.order.service.IOrderDetailService; import org.junit.Test; @@ -64,4 +65,12 @@ } @Test public void hashTest() { String tableTag = TableSuffix.ofHash("1236847735607631873",64,"-000").getSuffix(); System.out.println(tableTag); } }