MybatisPlus源码解析4:租户拦截器
本文主要是对租户拦截器进行解析,不同租户之间的数据隔离的,一个租户的数据查询、更新、插入、删除操作都不会对他的租户的数据产生任何影响
1.项目结构
源码地址:https://github.com/lmhdsad/mybatis-plus-source-study/tree/main/mybatis-plus-plugin-tenant
项目结构:

2. 源码分析 MybatisPlusInterceptor
@SuppressWarnings({
"rawtypes"})
@Intercepts(
{
@Signature(type = StatementHandler.class, method = "prepare", args = {
Connection.class, Integer.class}),
@Signature(type = StatementHandler.class, method = "getBoundSql", args = {
}),
@Signature(type = Executor.class, method = "update", args = {
MappedStatement.class, Object.class}),
@Signature(type = Executor.class, method = "query", args = {
MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
@Signature(type = Executor.class, method = "query", args = {
MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
}
)
public class MybatisPlusInterceptor implements Interceptor {
@Setter
private List<InnerInterceptor> interceptors = new ArrayList<>();
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object target = invocation.getTarget();
Object[] args = invocation.getArgs();
if (target instanceof Executor) {
final Executor executor = (Executor) target;
Object parameter = args[1];
boolean isUpdate = args.length == 2;
MappedStatement ms = (MappedStatement) args[0];
if (!isUpdate && ms.getSqlCommandType() == SqlCommandType.SELECT) {
RowBounds rowBounds = (RowBounds) args[2];
ResultHandler resultHandler = (ResultHandler) args[3];
<

文章解析了MybatisPlus中的租户拦截器工作原理,包括拦截器如何在查询时动态添加租户信息,以及解析和构建SQL表达式的过程。重点介绍了`TenantLineInnerInterceptor`的`beforeQuery`方法和`buildTableExpression`方法的应用。
最低0.47元/天 解锁文章
6600





