浏览代码

fixed gitee issues/IH776

= 7 年之前
父节点
当前提交
f90bc428f6

+ 42 - 0
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/annotations/SqlParser.java

@@ -0,0 +1,42 @@
+/**
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.annotations;
+
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * <p>
+ * 租户注解
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-01-13
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.TYPE, ElementType.METHOD})
+public @interface SqlParser {
+
+    /**
+     * <p>
+     * 过滤 SQL 解析,默认 false
+     * </p>
+     */
+    boolean filter() default false;
+}

+ 1 - 1
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/annotations/TableField.java

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
  * <p>
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  * use this file except in compliance with the License. You may obtain a copy of

+ 45 - 0
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/entity/SqlParserInfo.java

@@ -0,0 +1,45 @@
+/**
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.entity;
+
+
+/**
+ * <p>
+ * SQL 解析信息
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-01-13
+ */
+public class SqlParserInfo {
+
+    /**
+     * 过滤解析
+     */
+    private boolean filter;
+
+    public SqlParserInfo() {
+
+    }
+
+    public boolean getFilter() {
+        return filter;
+    }
+
+    public void setFilter(boolean filter) {
+        this.filter = filter;
+    }
+}

+ 1 - 1
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/entity/TableFieldInfo.java

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
  * <p>
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  * use this file except in compliance with the License. You may obtain a copy of

+ 6 - 0
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/plugins/SqlParserHandler.java

@@ -19,6 +19,7 @@ import java.util.List;
 
 import org.apache.ibatis.reflection.MetaObject;
 
+import com.baomidou.mybatisplus.entity.SqlParserInfo;
 import com.baomidou.mybatisplus.plugins.parser.ISqlParser;
 import com.baomidou.mybatisplus.plugins.parser.ISqlParserFilter;
 import com.baomidou.mybatisplus.plugins.parser.SqlInfo;
@@ -48,6 +49,11 @@ public abstract class SqlParserHandler {
             }
             // SQL 解析
             if (CollectionUtils.isNotEmpty(this.sqlParserList)) {
+                // @SqlParser(filter = true) 跳过该方法解析
+                SqlParserInfo sqlParserInfo = PluginUtils.getSqlParserInfo(metaObject);
+                if (sqlParserInfo.getFilter()) {
+                    return;
+                }
                 // 标记是否修改过 SQL
                 int flag = 0;
                 String originalSql = (String) metaObject.getValue(PluginUtils.DELEGATE_BOUNDSQL_SQL);

+ 51 - 0
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/toolkit/PluginUtils.java

@@ -15,13 +15,20 @@
  */
 package com.baomidou.mybatisplus.toolkit;
 
+import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
+import java.util.Map;
 import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.ibatis.mapping.MappedStatement;
 import org.apache.ibatis.reflection.MetaObject;
 import org.apache.ibatis.reflection.SystemMetaObject;
 
+import com.baomidou.mybatisplus.annotations.SqlParser;
+import com.baomidou.mybatisplus.entity.SqlParserInfo;
+import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
+
 /**
  * <p>
  * 插件工具类
@@ -35,10 +42,54 @@ public final class PluginUtils {
     public static final String DELEGATE_BOUNDSQL_SQL = "delegate.boundSql.sql";
     public static final String DELEGATE_MAPPEDSTATEMENT = "delegate.mappedStatement";
 
+    /**
+     * SQL 解析缓存
+     */
+    private static final Map<String, SqlParserInfo> sqlParserInfoCache = new ConcurrentHashMap<>();
+
+
     private PluginUtils() {
         // to do nothing
     }
 
+
+    /**
+     * <p>
+     * 获取 SqlParser 注解信息
+     * </p>
+     *
+     * @param metaObject 元数据对象
+     * @return
+     */
+    public static SqlParserInfo getSqlParserInfo(MetaObject metaObject) {
+        try {
+            MappedStatement ms = getMappedStatement(metaObject);
+            String statementId = ms.getId();
+            SqlParserInfo sqlParserInfo = sqlParserInfoCache.get(statementId);
+            if (null != sqlParserInfo) {
+                // 存在直接返回
+                return sqlParserInfo;
+            }
+            // 初始化缓存 SqlParser 注解信息
+            String namespace = statementId.substring(0, statementId.lastIndexOf("."));
+            Method[] methods = Class.forName(namespace).getDeclaredMethods();
+            for (Method method : methods) {
+                SqlParser sqlParser = method.getAnnotation(SqlParser.class);
+                sqlParserInfo = new SqlParserInfo();
+                if (null != sqlParser) {
+                    sqlParserInfo.setFilter(sqlParser.filter());
+                }
+                StringBuilder sid = new StringBuilder();
+                sid.append(namespace).append(".").append(method.getName());
+                sqlParserInfoCache.put(sid.toString(), sqlParserInfo);
+            }
+            // 获取缓存结果
+            return sqlParserInfoCache.get(statementId);
+        } catch (Exception e) {
+            throw new MybatisPlusException("获取 SqlParser 注解信息异常", e);
+        }
+    }
+
     /**
      * <p>
      * 获取当前执行 MappedStatement