This commit is contained in:
guoshengxiong 2024-05-28 14:05:51 +08:00
parent 5f4e04251d
commit 18f5be7b91

View File

@ -2,10 +2,7 @@ package com.zhgd.mybatis;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.parser.SqlParserHelper;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils; import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport; import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport;
@ -17,7 +14,6 @@ import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.schema.Table; import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.insert.Insert; import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.*; import net.sf.jsqlparser.statement.select.*;
import org.apache.commons.collections.MapUtils;
import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.BoundSql;
@ -92,18 +88,16 @@ public class DataScopeInterceptor extends JsqlParserSupport implements InnerInte
MappedStatement ms = mpSh.mappedStatement(); MappedStatement ms = mpSh.mappedStatement();
SqlCommandType sct = ms.getSqlCommandType(); SqlCommandType sct = ms.getSqlCommandType();
try { try {
if (sct == SqlCommandType.INSERT) { Class<?> clazz = Class.forName(ms.getId().substring(0, ms.getId().lastIndexOf(StringPool.DOT)));
Class<?> clazz = Class.forName(ms.getId().substring(0, ms.getId().lastIndexOf(StringPool.DOT))); String methodName = ms.getId().substring(ms.getId().lastIndexOf(".") + 1);
//注解判断 if (sct == SqlCommandType.SELECT) {
DataScope annotation = clazz.getAnnotation(DataScope.class); log.debug("查询mapper方法: {} >>> {}#{}", getCallPosition(), clazz.getName(), methodName);
if (annotation == null || annotation.type() == 2) { } else if (sct == SqlCommandType.UPDATE) {
return; log.debug("更新mapper方法: {} >>> {}#{}", getCallPosition(), clazz.getName(), methodName);
} } else if (sct == SqlCommandType.INSERT) {
if (InterceptorIgnoreHelper.willIgnoreTenantLine(ms.getId())) return; log.debug("插入mapper方法: {} >>> {}#{}", getCallPosition(), clazz.getName(), methodName);
if (SqlParserHelper.getSqlParserInfo(ms)) return; } else if (sct == SqlCommandType.DELETE) {
PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql(); log.debug("删除mapper方法: {} >>> {}#{}", getCallPosition(), clazz.getName(), methodName);
Map param = JSONObject.parseObject(JSON.toJSONString(mpBs.parameterObject()), Map.class);
mpBs.sql(parserMulti(mpBs.sql(), MapUtils.getString(param, "engineeringSn")));
} }
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
@ -126,7 +120,6 @@ public class DataScopeInterceptor extends JsqlParserSupport implements InnerInte
Method[] declaredMethods = clazz.getDeclaredMethods(); Method[] declaredMethods = clazz.getDeclaredMethods();
Optional<DataScope> dsOption = Arrays.stream(declaredMethods).filter(method -> method.getName().equals(methodName)).map(method -> method.getAnnotation(DataScope.class)).filter(Objects::nonNull).findFirst(); Optional<DataScope> dsOption = Arrays.stream(declaredMethods).filter(method -> method.getName().equals(methodName)).map(method -> method.getAnnotation(DataScope.class)).filter(Objects::nonNull).findFirst();
annotation = dsOption.orElseGet(() -> clazz.getAnnotation(DataScope.class)); annotation = dsOption.orElseGet(() -> clazz.getAnnotation(DataScope.class));
log.debug("查询mapper方法: {} >>> {}#{}", getCallPosition(), clazz.getName(), methodName);
if (findIgnoreDataScope(parameter, annotation) && isNotSqlTest()) { if (findIgnoreDataScope(parameter, annotation) && isNotSqlTest()) {
return; return;
} }