配置相关信息:
pring.shardingsphere.datasource.names=btspismp spring.shardingsphere.datasource.btspismp.driver-class-name=net.sf.log4jdbc.DriverSpy spring.shardingsphere.datasource.btspismp.jdbc-url=jdbc:log4jdbc:mysql://192.168.115.31:3306/btspIsmp?characterEncoding=UTF-8&autoReconnect=true&useUnicode=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true&rewriteBatchedStatements=true spring.shardingsphere.datasource.btspismp.username=root spring.shardingsphere.datasource.btspismp.password=123456 #状态变更及属性分表 spring.shardingsphere.sharding.tables.T_CONTRACT_PRODUCT_STATUS_CHANG_RECORD.table-strategy.inline.sharding-column=order_id spring.shardingsphere.sharding.tables.T_CONTRACT_PRODUCT_STATUS_CHANG_RECORD.table-strategy.inline.algorithm-expression=T_CONTRACT_PRODUCT_STATUS_CHANG_RECORD_$->{order_id %10} spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT_STATUS_CHANG_RECORD.table-strategy.inline.sharding-column=order_id spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT_STATUS_CHANG_RECORD.table-strategy.inline.algorithm-expression=T_ORDER_ACCEPT_STATUS_CHANG_RECORD_$->{order_id %10} spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT_SPS_ATTR_VALUE.table-strategy.inline.sharding-column=order_id spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT_SPS_ATTR_VALUE.table-strategy.inline.algorithm-expression=T_ORDER_ACCEPT_SPS_ATTR_VALUE_$->{order_id %10} #历史分表 spring.shardingsphere.sharding.tables.T_CONTRACT_PRODUCT_STATUS_CHANG_RECORD_HIS.actual-data-nodes=btspismp.T_CONTRACT_PRODUCT_STATUS_CHANG_RECORD_${0..9}_HIS spring.shardingsphere.sharding.tables.T_CONTRACT_PRODUCT_STATUS_CHANG_RECORD_HIS.table-strategy.standard.sharding-column=order_id spring.shardingsphere.sharding.tables.T_CONTRACT_PRODUCT_STATUS_CHANG_RECORD_HIS.table-strategy.standard.precise-algorithm-class-name=config.ContractStatusChangeRecordHisTableShardingConfig spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT_SPS_ATTR_VALUE_HIS.actual-data-nodes=btspismp.T_ORDER_ACCEPT_SPS_ATTR_VALUE_${0..9}_HIS spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT_SPS_ATTR_VALUE_HIS.table-strategy.standard.sharding-column=order_id spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT_SPS_ATTR_VALUE_HIS.table-strategy.standard.precise-algorithm-class-name=config.OrderAcceptSpsAttrHisTableShardingConfig spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT_STATUS_CHANG_RECORD_HIS.actual-data-nodes=btspismp.T_ORDER_ACCEPT_STATUS_CHANG_RECORD_${0..9}_HIS spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT_STATUS_CHANG_RECORD_HIS.table-strategy.standard.sharding-column=order_id spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT_STATUS_CHANG_RECORD_HIS.table-strategy.standard.precise-algorithm-class-name=config.OrderAcceptStatusChangeRecordHisTableShardingConfig #工单表分表 spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT.actual-data-nodes=btspismp.T_ORDER_ACCEPT,btspismp.T_ORDER_ACCEPT_CRBT spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT.table-strategy.standard.sharding-column=table_id spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT.table-strategy.standard.precise-algorithm-class-name=config.OrderAcceptTableShardingConfig #工单历史表分表 spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT_HIS.actual-data-nodes=btspismp.T_ORDER_ACCEPT_HIS,btspismp.T_ORDER_ACCEPT_HIS_CRBT spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT_HIS.table-strategy.standard.sharding-column=table_id spring.shardingsphere.sharding.tables.T_ORDER_ACCEPT_HIS.table-strategy.standard.precise-algorithm-class-name=config.OrderAcceptHisTableShardingConfig spring.shardingsphere.datasource.btspismp.type=com.zaxxer.hikari.HikariDataSource spring.datasource.hikari.minimum-idle=5 spring.datasource.hikari.maximum-pool-size=15 spring.datasource.hikari.auto-commit=true spring.datasource.hikari.idle-timeout=30000 spring.datasource.hikari.pool-name=DatebookHikariCP spring.datasource.hikari.max-lifetime=30000 spring.datasource.hikari.connection-timeout=30000 spring.datasource.hikari.connection-test-query=SELECT 1 spring.datasource.hikari.leak-detection-threshold=1000 #JPA spring.jpa.database=MYSQL spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=none spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect #spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
代码:
package config; import java.util.Collection; import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm; import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Component public class OrderAcceptTableShardingConfig implements PreciseShardingAlgorithm<String> { private Logger log = LoggerFactory.getLogger(this.getClass()); @Override public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) { /* * for (String tableName : availableTargetNames) { log.info("=====tableName:" + * tableName); } */ //log.info("=====shardingValue:" + shardingValue); //log.info("====logic table name:" + shardingValue.getLogicTableName()); //log.info("====column name:" + shardingValue.getColumnName()); //log.info("====sharding value String:" + shardingValue.toString()); int start = shardingValue.toString().indexOf("value="); int end = shardingValue.toString().indexOf(")"); //log.info("length" + shardingValue.toString().length() + ",start:" + start + ",end:" + end); //log.info("Value:" + shardingValue.toString().substring(start + 6, end)); String valueString = shardingValue.toString().substring(start + 6, end); if (valueString.contentEquals("1")) { return "T_ORDER_ACCEPT_CRBT"; } else if (valueString.contentEquals("2")) { return "T_ORDER_ACCEPT_CRBT"; } else if (valueString.contentEquals("3")) { return "T_ORDER_ACCEPT_PCA"; } else if (valueString.contentEquals("4")) { return "T_ORDER_ACCEPT_PRE_PCA"; } else if (valueString.contentEquals("5")) { return "T_ORDER_ACCEPT_MPCA"; } else if (valueString.contentEquals("6")) { return "T_ORDER_ACCEPT_PRE_MPCA"; } else if (valueString.contentEquals("7")) { return "T_ORDER_ACCEPT_MCA"; } else { return "T_ORDER_ACCEPT"; } } }