水平分表(Mybatis插件实现)
tips:(这里可以根据实际业务进行选择,比如每个月数据不是很平均,就可以按照预算数据量来进行分割
例子,按照id mod 100 把表分成100张,然后进行表名+ (id mod 100)取 )
这里为了简单起见,假定月份数据量相差不大
将一张大表按照月份划分
fee_202001,fee_202002,fee_202003…
表中只有三个字段:id,费用金额fee_amt,费用日期fee_date
其中gmt_create,gmt_modified是阿里出的java泰山
版表定义的规范
需要注意的是插件的注册,只需要在自定义插件上添加@Component,如果是Mybatis单独使用,则需要单独去注册,我这里是配合Spring
核心就在于拦截Executor
,这里是只拦截了查询所以是 method添加的query 而且为了简单起见,并没有对缓存做特殊处理,也就是经过这个拦截器执行的开启了缓存也是没有效果的,如果想要生效,则去复写Exector中的另一个方法
核心类MyInterceptor
package com.example.interceptor;
import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.Properties;
//Spring项目 添加Component注解 就是插件的注册
@Component
@Intercepts({
@Signature(type = Executor.class,method = "query",
args = {
MappedStatement.class