Bläddra i källkod

调整插件忽略执行优先级.

nieqiurong 4 månader sedan
förälder
incheckning
d2a72c73bb

+ 14 - 8
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/override/MybatisMapperProxy.java

@@ -166,16 +166,22 @@ public class MybatisMapperProxy<T> implements InvocationHandler, Serializable {
 
         @Override
         public Object invoke(Object proxy, Method method, Object[] args, SqlSession sqlSession) throws Throwable {
-            try {
-                MybatisMapperProxy<?> mybatisMapperProxy = MybatisUtils.getMybatisMapperProxy(proxy);
-                Class<?> mapperInterface = mybatisMapperProxy.getMapperInterface();
-                IgnoreStrategy ignoreStrategy = InterceptorIgnoreHelper.findIgnoreStrategy(mapperInterface, method);
-                if (ignoreStrategy != null) {
+            boolean hasIgnoreStrategy = InterceptorIgnoreHelper.hasIgnoreStrategy();
+            if (hasIgnoreStrategy) {
+                return methodHandle.bindTo(proxy).invokeWithArguments(args);
+            } else {
+                try {
+                    MybatisMapperProxy<?> mybatisMapperProxy = MybatisUtils.getMybatisMapperProxy(proxy);
+                    Class<?> mapperInterface = mybatisMapperProxy.getMapperInterface();
+                    IgnoreStrategy ignoreStrategy = InterceptorIgnoreHelper.findIgnoreStrategy(mapperInterface, method);
+                    if (ignoreStrategy == null) {
+                        ignoreStrategy = IgnoreStrategy.DEFAULT;
+                    }
                     InterceptorIgnoreHelper.handle(ignoreStrategy);
+                    return methodHandle.bindTo(proxy).invokeWithArguments(args);
+                } finally {
+                    InterceptorIgnoreHelper.clearIgnoreStrategy();
                 }
-                return methodHandle.bindTo(proxy).invokeWithArguments(args);
-            } finally {
-                InterceptorIgnoreHelper.clearIgnoreStrategy();
             }
         }
     }

+ 6 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/plugins/IgnoreStrategy.java

@@ -25,6 +25,12 @@ import java.util.Map;
 @Setter
 @Builder
 public class IgnoreStrategy {
+
+    /**
+     * @since 3.5.10
+     */
+    public static final IgnoreStrategy DEFAULT = IgnoreStrategy.builder().build();
+
     private Boolean tenantLine;
     private Boolean dynamicTableName;
     private Boolean blockAttack;

+ 11 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/plugins/InterceptorIgnoreHelper.java

@@ -66,6 +66,16 @@ public abstract class InterceptorIgnoreHelper {
         IGNORE_STRATEGY_LOCAL.remove();
     }
 
+    /**
+     * 判断当前线程是否有忽略策略
+     *
+     * @return 是否有忽略策略
+     * @since 3.5.10
+     */
+    public static boolean hasIgnoreStrategy() {
+        return IGNORE_STRATEGY_LOCAL.get() != null;
+    }
+
     /**
      * 初始化缓存
      * <p>
@@ -96,7 +106,7 @@ public abstract class InterceptorIgnoreHelper {
     }
 
     /**
-     * 按指定策略执行指定方法
+     * 按指定策略执行指定方法 (忽略线程级别,参数执行级使用最高)
      *
      * @param ignoreStrategy 忽略策略
      * @param supplier       执行方法

+ 33 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/tenant/EntityMapper.java

@@ -2,6 +2,8 @@ package com.baomidou.mybatisplus.test.tenant;
 
 import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
+import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
 import org.apache.ibatis.annotations.CacheNamespace;
 
 import java.io.Serializable;
@@ -34,6 +36,37 @@ public interface EntityMapper extends BaseMapper<Entity> {
         return selectById(id);
     }
 
+    @InterceptorIgnore(tenantLine = "false")
+    default Entity selectByIdWithIgnore2(Serializable id) {
+        return selectByIdWithIgnore(id);
+    }
+
+    default Entity selectByIdWithIgnore3(Serializable id) {
+        return selectByIdWithIgnore(id);
+    }
+
+    default Entity selectByIdWithIgnore4(Serializable id) {
+        return selectByIdWithIgnore2(id);
+    }
+
+    default Entity selectByIdWithIgnore5(Serializable id) {
+        return selectById(id);
+    }
+
+    default Entity selectByIdWithIgnore6(IgnoreStrategy ignoreStrategy, Serializable id) {
+        return InterceptorIgnoreHelper.execute(ignoreStrategy, ()-> selectById(id));
+    }
+
+    default Entity selectByIdWithIgnore7(IgnoreStrategy ignoreStrategy, Serializable id) {
+        return InterceptorIgnoreHelper.execute(ignoreStrategy, ()-> selectByIdWithIgnore(id));
+    }
+
+    default Entity selectByIdWithIgnore8(IgnoreStrategy ignoreStrategy, Serializable id) {
+        return InterceptorIgnoreHelper.execute(ignoreStrategy, ()-> selectByIdWithIgnore2(id));
+    }
+
+
+
 //    /**
 //     *  //TODO 由于是对ms级别的忽略,所以不考虑重载方法, 忽略deleteById方法
 //     */

+ 22 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/tenant/TenantTest.java

@@ -91,6 +91,28 @@ public class TenantTest extends BaseDbTest<EntityMapper> {
             m.insert(entity);
             Assertions.assertNull(m.selectById(entity.getId()));
             Assertions.assertNotNull(m.selectByIdWithIgnore(entity.getId()));
+            Assertions.assertNull(m.selectByIdWithIgnore2(entity.getId()));
+            Assertions.assertNull(m.selectByIdWithIgnore3(entity.getId()));
+            Assertions.assertNull(m.selectByIdWithIgnore4(entity.getId()));
+            Assertions.assertNull(m.selectByIdWithIgnore5(entity.getId()));
+
+            Assertions.assertNotNull(m.selectByIdWithIgnore6(IgnoreStrategy.builder().tenantLine(true).build(), entity.getId()));
+            Assertions.assertNull(m.selectByIdWithIgnore6(IgnoreStrategy.builder().tenantLine(false).build(), entity.getId()));
+            Assertions.assertNotNull(m.selectByIdWithIgnore7(IgnoreStrategy.builder().tenantLine(true).build(), entity.getId()));
+            Assertions.assertNull(m.selectByIdWithIgnore7(IgnoreStrategy.builder().tenantLine(false).build(), entity.getId()));
+            Assertions.assertNotNull(m.selectByIdWithIgnore8(IgnoreStrategy.builder().tenantLine(true).build(), entity.getId()));
+            Assertions.assertNull(m.selectByIdWithIgnore8(IgnoreStrategy.builder().tenantLine(false).build(), entity.getId()));
+
+            Assertions.assertNotNull(InterceptorIgnoreHelper.execute(IgnoreStrategy.builder().tenantLine(true).build(),()->m.selectByIdWithIgnore(entity.getId())));
+            Assertions.assertNull(InterceptorIgnoreHelper.execute(IgnoreStrategy.builder().tenantLine(false).build(),()->m.selectByIdWithIgnore(entity.getId())));
+            Assertions.assertNotNull(InterceptorIgnoreHelper.execute(IgnoreStrategy.builder().tenantLine(true).build(),()->m.selectByIdWithIgnore2(entity.getId())));
+            Assertions.assertNull(InterceptorIgnoreHelper.execute(IgnoreStrategy.builder().tenantLine(false).build(),()->m.selectByIdWithIgnore2(entity.getId())));
+            Assertions.assertNotNull(InterceptorIgnoreHelper.execute(IgnoreStrategy.builder().tenantLine(true).build(),()->m.selectByIdWithIgnore3(entity.getId())));
+            Assertions.assertNull(InterceptorIgnoreHelper.execute(IgnoreStrategy.builder().tenantLine(false).build(),()->m.selectByIdWithIgnore3(entity.getId())));
+            Assertions.assertNotNull(InterceptorIgnoreHelper.execute(IgnoreStrategy.builder().tenantLine(true).build(),()->m.selectByIdWithIgnore4(entity.getId())));
+            Assertions.assertNull(InterceptorIgnoreHelper.execute(IgnoreStrategy.builder().tenantLine(false).build(),()->m.selectByIdWithIgnore4(entity.getId())));
+            Assertions.assertNotNull(InterceptorIgnoreHelper.execute(IgnoreStrategy.builder().tenantLine(true).build(),()->m.selectByIdWithIgnore5(entity.getId())));
+            Assertions.assertNull(InterceptorIgnoreHelper.execute(IgnoreStrategy.builder().tenantLine(false).build(),()->m.selectByIdWithIgnore5(entity.getId())));
             Assertions.assertEquals(0, m.deleteById(entity.getId()));
             Assertions.assertEquals(1, m.deleteByIdWithIgnore(entity.getId()));
         });