|
@@ -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
|