第一步:引入依赖文件
<!--当当网分库分表-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
第二步:工具类
@Component
public class PayRecordPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Date> {
@Override
public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
String targetTable = preciseShardingValue.getLogicTableName() + "_" + DateFormatUtil.format(new Date(), "yyyyMM");
if (collection.contains(targetTable)) {
return targetTable;
}
throw new UnsupportedOperationException("无效的表名称: " + targetTable);