فهرست منبع

新增支持手动拦截器忽略策略

hubin 2 سال پیش
والد
کامیت
f12ca79165

+ 1 - 0
mybatis-plus-boot-starter/src/main/java/com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.java

@@ -385,6 +385,7 @@ public class MybatisPlusAutoConfiguration implements InitializingBean {
 
     @Order
     @Bean
+    @ConditionalOnMissingBean
     public DdlApplicationRunner ddlApplicationRunner(@Autowired(required = false) List<IDdl> ddlList) {
         if (ObjectUtils.isEmpty(ddlList)) {
             return null;

+ 3 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisMapperAnnotationBuilder.java

@@ -16,6 +16,7 @@
 package com.baomidou.mybatisplus.core;
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
 import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
 import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
@@ -97,7 +98,7 @@ public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
             assistant.setCurrentNamespace(mapperName);
             parseCache();
             parseCacheRef();
-            InterceptorIgnoreHelper.InterceptorIgnoreCache cache = InterceptorIgnoreHelper.initSqlParserInfoCache(type);
+            IgnoreStrategy ignoreStrategy = InterceptorIgnoreHelper.initSqlParserInfoCache(type);
             for (Method method : type.getMethods()) {
                 if (!canHaveStatement(method)) {
                     continue;
@@ -108,7 +109,7 @@ public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
                 }
                 try {
                     // TODO 加入 注解过滤缓存
-                    InterceptorIgnoreHelper.initSqlParserInfoCache(cache, mapperName, method);
+                    InterceptorIgnoreHelper.initSqlParserInfoCache(ignoreStrategy, mapperName, method);
                     parseStatement(method);
                 } catch (IncompleteElementException e) {
                     // TODO 使用 MybatisMethodResolver 而不是 MethodResolver

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

@@ -0,0 +1,20 @@
+package com.baomidou.mybatisplus.core.plugins;
+
+import lombok.Builder;
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.Map;
+
+@Getter
+@Setter
+@Builder
+public class IgnoreStrategy {
+    private Boolean tenantLine;
+    private Boolean dynamicTableName;
+    private Boolean blockAttack;
+    private Boolean illegalSql;
+    private Boolean dataPermission;
+    private Map<String, Boolean> others;
+
+}

+ 58 - 40
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/plugins/InterceptorIgnoreHelper.java

@@ -17,8 +17,6 @@ package com.baomidou.mybatisplus.core.plugins;
 
 import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
 import com.baomidou.mybatisplus.core.toolkit.*;
-import lombok.Builder;
-import lombok.Data;
 import org.apache.ibatis.executor.keygen.SelectKeyGenerator;
 
 import java.lang.reflect.Method;
@@ -38,7 +36,33 @@ public abstract class InterceptorIgnoreHelper {
      * SQL 解析缓存
      * key 可能是 mappedStatement 的 ID,也可能是 class 的 name
      */
-    private static final Map<String, InterceptorIgnoreCache> INTERCEPTOR_IGNORE_CACHE = new ConcurrentHashMap<>();
+    private static final Map<String, IgnoreStrategy> IGNORE_STRATEGY_CACHE = new ConcurrentHashMap<>();
+    /**
+     *  本地线程拦截器忽略策略缓存
+     */
+    private static final ThreadLocal<IgnoreStrategy> IGNORE_STRATEGY_LOCAL = new ThreadLocal<>();
+
+    /**
+     * 手动设置拦截器忽略执行策略,权限大于注解权限
+     * <p>
+     * InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
+     * </p>
+     * <p>
+     * 注意,需要手动关闭调用方法 InterceptorIgnoreHelper.clearIgnoreStrategy();
+     * </p>
+     *
+     * @param ignoreStrategy {@link IgnoreStrategy}
+     */
+    public static void handle(IgnoreStrategy ignoreStrategy) {
+        IGNORE_STRATEGY_LOCAL.set(ignoreStrategy);
+    }
+
+    /**
+     * 清空本地忽略策略
+     */
+    public static void clearIgnoreStrategy() {
+        IGNORE_STRATEGY_LOCAL.remove();
+    }
 
     /**
      * 初始化缓存
@@ -47,12 +71,12 @@ public abstract class InterceptorIgnoreHelper {
      *
      * @param mapperClass Mapper Class
      */
-    public synchronized static InterceptorIgnoreCache initSqlParserInfoCache(Class<?> mapperClass) {
+    public synchronized static IgnoreStrategy initSqlParserInfoCache(Class<?> mapperClass) {
         InterceptorIgnore ignore = mapperClass.getAnnotation(InterceptorIgnore.class);
         if (ignore != null) {
             String key = mapperClass.getName();
-            InterceptorIgnoreCache cache = buildInterceptorIgnoreCache(key, ignore);
-            INTERCEPTOR_IGNORE_CACHE.put(key, cache);
+            IgnoreStrategy cache = buildIgnoreStrategy(key, ignore);
+            IGNORE_STRATEGY_CACHE.put(key, cache);
             return cache;
         }
         return null;
@@ -66,62 +90,67 @@ public abstract class InterceptorIgnoreHelper {
      * @param mapperAnnotation Mapper Class Name
      * @param method           Method
      */
-    public static void initSqlParserInfoCache(InterceptorIgnoreCache mapperAnnotation, String mapperClassName, Method method) {
-        InterceptorIgnore ignore = method.getAnnotation(InterceptorIgnore.class);
+    public static void initSqlParserInfoCache(IgnoreStrategy mapperAnnotation, String mapperClassName, Method method) {
+        InterceptorIgnore ignoreStrategy = method.getAnnotation(InterceptorIgnore.class);
         String key = mapperClassName.concat(StringPool.DOT).concat(method.getName());
         String name = mapperClassName.concat(StringPool.HASH).concat(method.getName());
-        if (ignore != null) {
-            InterceptorIgnoreCache methodCache = buildInterceptorIgnoreCache(name, ignore);
+        if (ignoreStrategy != null) {
+            IgnoreStrategy methodCache = buildIgnoreStrategy(name, ignoreStrategy);
             if (mapperAnnotation == null) {
-                INTERCEPTOR_IGNORE_CACHE.put(key, methodCache);
+                IGNORE_STRATEGY_CACHE.put(key, methodCache);
                 return;
             }
-            INTERCEPTOR_IGNORE_CACHE.put(key, chooseCache(mapperAnnotation, methodCache));
+            IGNORE_STRATEGY_CACHE.put(key, chooseCache(mapperAnnotation, methodCache));
         }
     }
 
     public static boolean willIgnoreTenantLine(String id) {
-        return willIgnore(id, InterceptorIgnoreCache::getTenantLine);
+        return willIgnore(id, IgnoreStrategy::getTenantLine);
     }
 
     public static boolean willIgnoreDynamicTableName(String id) {
-        return willIgnore(id, InterceptorIgnoreCache::getDynamicTableName);
+        return willIgnore(id, IgnoreStrategy::getDynamicTableName);
     }
 
     public static boolean willIgnoreBlockAttack(String id) {
-        return willIgnore(id, InterceptorIgnoreCache::getBlockAttack);
+        return willIgnore(id, IgnoreStrategy::getBlockAttack);
     }
 
     public static boolean willIgnoreIllegalSql(String id) {
-        return willIgnore(id, InterceptorIgnoreCache::getIllegalSql);
+        return willIgnore(id, IgnoreStrategy::getIllegalSql);
     }
 
     public static boolean willIgnoreDataPermission(String id) {
-        return willIgnore(id, InterceptorIgnoreCache::getDataPermission);
+        return willIgnore(id, IgnoreStrategy::getDataPermission);
     }
 
     public static boolean willIgnoreOthersByKey(String id, String key) {
         return willIgnore(id, i -> CollectionUtils.isNotEmpty(i.getOthers()) && i.getOthers().getOrDefault(key, false));
     }
 
-    public static boolean willIgnore(String id, Function<InterceptorIgnoreCache, Boolean> function) {
-        InterceptorIgnoreCache cache = INTERCEPTOR_IGNORE_CACHE.get(id);
-        if (cache == null && id.endsWith(SelectKeyGenerator.SELECT_KEY_SUFFIX)) {
+    public static boolean willIgnore(String id, Function<IgnoreStrategy, Boolean> function) {
+        // 1,优化获取本地忽略策略
+        IgnoreStrategy ignoreStrategy = IGNORE_STRATEGY_LOCAL.get();
+        if (null == ignoreStrategy) {
+            // 2,不存在取注解策略
+            ignoreStrategy = IGNORE_STRATEGY_CACHE.get(id);
+        }
+        if (ignoreStrategy == null && id.endsWith(SelectKeyGenerator.SELECT_KEY_SUFFIX)) {
             // 支持一下 selectKey
-            cache = INTERCEPTOR_IGNORE_CACHE.get(id.substring(0, id.length() - SelectKeyGenerator.SELECT_KEY_SUFFIX.length()));
+            ignoreStrategy = IGNORE_STRATEGY_CACHE.get(id.substring(0, id.length() - SelectKeyGenerator.SELECT_KEY_SUFFIX.length()));
         }
-        if (cache == null) {
-            cache = INTERCEPTOR_IGNORE_CACHE.get(id.substring(0, id.lastIndexOf(StringPool.DOT)));
+        if (ignoreStrategy == null) {
+            ignoreStrategy = IGNORE_STRATEGY_CACHE.get(id.substring(0, id.lastIndexOf(StringPool.DOT)));
         }
-        if (cache != null) {
-            Boolean apply = function.apply(cache);
+        if (ignoreStrategy != null) {
+            Boolean apply = function.apply(ignoreStrategy);
             return apply != null && apply;
         }
         return false;
     }
 
-    private static InterceptorIgnoreCache chooseCache(InterceptorIgnoreCache mapper, InterceptorIgnoreCache method) {
-        return InterceptorIgnoreCache.builder()
+    private static IgnoreStrategy chooseCache(IgnoreStrategy mapper, IgnoreStrategy method) {
+        return IgnoreStrategy.builder()
             .tenantLine(chooseBoolean(mapper.getTenantLine(), method.getTenantLine()))
             .dynamicTableName(chooseBoolean(mapper.getDynamicTableName(), method.getDynamicTableName()))
             .blockAttack(chooseBoolean(mapper.getBlockAttack(), method.getBlockAttack()))
@@ -131,8 +160,8 @@ public abstract class InterceptorIgnoreHelper {
             .build();
     }
 
-    private static InterceptorIgnoreCache buildInterceptorIgnoreCache(String name, InterceptorIgnore ignore) {
-        return InterceptorIgnoreCache.builder()
+    private static IgnoreStrategy buildIgnoreStrategy(String name, InterceptorIgnore ignore) {
+        return IgnoreStrategy.builder()
             .tenantLine(getBoolean("tenantLine", name, ignore.tenantLine()))
             .dynamicTableName(getBoolean("dynamicTableName", name, ignore.dynamicTableName()))
             .blockAttack(getBoolean("blockAttack", name, ignore.blockAttack()))
@@ -204,15 +233,4 @@ public abstract class InterceptorIgnoreHelper {
         methodKeys.forEach(k -> map.put(k, chooseBoolean(mapper.get(k), method.get(k))));
         return map;
     }
-
-    @Data
-    @Builder
-    public static class InterceptorIgnoreCache {
-        private Boolean tenantLine;
-        private Boolean dynamicTableName;
-        private Boolean blockAttack;
-        private Boolean illegalSql;
-        private Boolean dataPermission;
-        private Map<String, Boolean> others;
-    }
 }

+ 2 - 2
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/plugins/InterceptorIgnoreHelperTest.java

@@ -46,9 +46,9 @@ class InterceptorIgnoreHelperTest {
     }
 
     private void init(Class<?> clazz) {
-        InterceptorIgnoreHelper.InterceptorIgnoreCache cache = InterceptorIgnoreHelper.initSqlParserInfoCache(clazz);
+        IgnoreStrategy ignoreStrategy = InterceptorIgnoreHelper.initSqlParserInfoCache(clazz);
         for (Method method : clazz.getMethods()) {
-            InterceptorIgnoreHelper.initSqlParserInfoCache(cache, clazz.getName(), method);
+            InterceptorIgnoreHelper.initSqlParserInfoCache(ignoreStrategy, clazz.getName(), method);
         }
     }
 

+ 0 - 1
mybatis-plus-extension/build.gradle

@@ -1,4 +1,3 @@
-apply plugin: 'kotlin'
 
 dependencies {
     api project(":mybatis-plus-core")