package com.kidgrow.db.sharding; import org.apache.commons.lang.StringUtils; /** * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020
* * @Description: 按照字符串Hash求余确定分表
* @Project:
* @CreateDate: Created in 2020/2/4 16:05
* @Author: liuke */ public class HashModTableSuffix implements TableSuffix { private String hash; private Long tableSize = 64L; private String tableSuffix="-000"; 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() { String format = "%0" + (tableSuffix.length() - 1) + "d"; return "_"+String.format(format, Math.abs(hash.hashCode()) % tableSize); } }