建表
create table sign_{0,9}{
xxx
}
定制策略
创建ModShardingStrategy 实现 AbstractShardingStrategy
修改mapper
mapper 添加注解
@TableSharding(tableName = "activity_xxx", shardBy = "uid",strategy = "com.xxx.ModShardingStrategy")
添加拦截器
@Bean("sqlSessionFactory")
@Primary
public SqlSessionFactory buildSqlSessionFactory() {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setPlugins(new Interceptor[] {commonSqlInterceptor()});
bean.setConfiguration(mybatisConfiguration());
//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath:mapping/activity/*.xml"));
return bean.getObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Bean(name = "couponCommonSqlInterceptor")
@Primary
public Interceptor commonSqlInterceptor() {
CommonSqlInterceptor tableShardingInterceptor = new CommonSqlInterceptor();
Properties properties = new Properties();
if ("dev".equals(env) || "test".equals(env) || "emulate".equals(env) || "uat".equals(env)) {
properties.setProperty("show_traceId", "true");
properties.setProperty("enable_sharding", "true");
} else {
properties.setProperty("show_traceId", "true"); //开启trace追踪
properties.setProperty("enable_sharding", "true");//开启分表 结合@TableSharding类注解
//默认分表策略
properties.setProperty("shardingTables", "coupon_xxx_record");
}
tableShardingInterceptor.setProperties(properties);
return tableShardingInterceptor;
}