MybatisPlus源码解析4:租户拦截器

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

本文主要是对租户拦截器进行解析,不同租户之间的数据隔离的,一个租户的数据查询、更新、插入、删除操作都不会对他的租户的数据产生任何影响

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];
                <
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值