Mybatis-plus 自定义拦截器动态修改sql

import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;
import net.sf.jsqlparser.statement.select.SetOperationList;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.stereotype.Component;
​
import java.io.StringReader;
import java.sql.Connection;
import java.util.List;
import java.util.Properties;
​
@Slf4j
@AllArgsConstructor
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
@Component
public class MybatisSqlInterceptor implements Interceptor {
​
​
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
        MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
        //this.sqlParser(metaObject);
        // 先判断是不是SELECT操作 不是直接过滤
        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
        if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
            return invocation.proceed();
        }
        BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
        // 执行的SQL语句
        String originalSql = boundSql.getSql();
​
        // SQL语句的参数
        Object parameterObject = boundSql.getParameterObject();
​
        // 重新处理sql 语句
        String finalSql = this.handleSql(originalSql, "equity_code", "E0068000022");
​
         System.out.println("处理后的sql:" + finalSql);
​
        //  originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + 1 + " in (" + 2 + ")";
        metaObject.setValue("delegate.boundSql.sql", finalSql);
        return invocation.proceed();
    }
​
​
    /**
     * PlainSelect 和 SetOperationList 是 JavaCC 生成的类,用于表示 SQL 查询语句中的 SELECT 子句和 UNION/UNION ALL 等关键字后面的查询语句。
     *
     * 具体来说,PlainSelect 类表示一个简单的 SELECT 查询语句,可以包含多个表、列、JOIN 子句、WHERE 子句等。而 SetOperationList 类表示一个               UNION、INTERSECT 或 EXCEPT 操作符连接多个查询语句,可以有多个 PlainSelect 子句组成。
     * @param originalSql
     * @param key
     * @param value
     * @return
     * @throws JSQLParserException
     */
    private String handleSql(String originalSql, String key, String value) throws JSQLParserException {
        CCJSqlParserManager parserManager = new CCJSqlParserManager();
        Select select = (Select) parserManager.parse(new StringReader(originalSql));
        SelectBody selectBody = select.getSelectBody();
        if (selectBody instanceof PlainSelect) {
            this.setWhere((PlainSelect) selectBody, key, value);
        } else if (selectBody instanceof SetOperationList) {
            SetOperationList setOperationList = (SetOperationList) selectBody;
            List selectBodyList = setOperationList.getSelects();
            selectBodyList.forEach(s -> this.setWhere((PlainSelect) s, key, value));
        }
        return select.toString();
    }
​
    /**
     * 设置 where 条件  --  使用CCJSqlParser将原SQL进行解析并改写
     *
     * @param plainSelect 查询对象
     */
    @SneakyThrows(Exception.class)
    protected void setWhere(PlainSelect plainSelect, String key, String value) {
        Table fromItem = (Table) plainSelect.getFromItem();
        // 有别名用别名,无别名用表名,防止字段冲突报错
        Alias fromItemAlias = fromItem.getAlias();
        String mainTableName = fromItemAlias == null ? fromItem.getName() : fromItemAlias.getName();
        // 构建子查询 -- 数据权限过滤SQL
        String dataPermissionSql = "";
​
        EqualsTo selfEqualsTo = new EqualsTo();
        selfEqualsTo.setLeftExpression(new Column(mainTableName + "." + key));
        selfEqualsTo.setRightExpression(new StringValue(value));
        dataPermissionSql = selfEqualsTo.toString();
​
        // dataPermissionSql = mainTableName + "." + key + " in ( " + CollUtil.join(new HashSet<>(), StringPool.COMMA) + " )";
​
        if (plainSelect.getWhere() == null) {
            plainSelect.setWhere(CCJSqlParserUtil.parseCondExpression(dataPermissionSql));
        } else {
            plainSelect.setWhere(new AndExpression(plainSelect.getWhere(), CCJSqlParserUtil.parseCondExpression(dataPermissionSql)));
        }
    }
​
​
    /**
     * 生成拦截对象的代理
     *
     * @param target 目标对象
     * @return 代理对象
     */
    @Override
    public Object plugin(Object target) {
        if (target instanceof StatementHandler) {
            return Plugin.wrap(target, this);
        }
        return target;
    }
​
    /**
     * mybatis配置的属性
     *
     * @param properties mybatis配置的属性
     */
    @Override
    public void setProperties(Properties properties) {
​
    }
}

1.核心解析

@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})

在 MyBatis 中,可以通过使用 @Intercepts 和 @Signature 注解来定义拦截器和拦截点。其中,@Signature 注解用于指定要拦截的类和方法,以及方法的参数类型。如果需要新增拦截参数,可以在 args 属性中添加新的参数类型。
​
public interface StatementHandler {
  Statement prepare(Connection connection, Integer transactionTimeout)
      throws SQLException;
​
  void parameterize(Statement statement)
      throws SQLException;
​
  void batch(Statement statement)
      throws SQLException;
​
  int update(Statement statement)
      throws SQLException;
​
   List query(Statement statement, ResultHandler resultHandler)
      throws SQLException;
​
   Cursor queryCursor(Statement statement)
      throws SQLException;
​
  BoundSql getBoundSql();
​
  ParameterHandler getParameterHandler();
​
}

2.SQL 处理点

/**
     * 设置 where 条件  --  使用CCJSqlParser将原SQL进行解析并改写
     *
     * @param plainSelect 查询对象
     */
    @SneakyThrows(Exception.class)
    protected void setWhere(PlainSelect plainSelect, String key, String value) {
        Table fromItem = (Table) plainSelect.getFromItem();
        // 有别名用别名,无别名用表名,防止字段冲突报错
        Alias fromItemAlias = fromItem.getAlias();
        String mainTableName = fromItemAlias == null ? fromItem.getName() : fromItemAlias.getName();
        // 构建子查询 -- 数据权限过滤SQL
        String dataPermissionSql = "";
​
        EqualsTo selfEqualsTo = new EqualsTo();
        selfEqualsTo.setLeftExpression(new Column(mainTableName + "." + key));
        selfEqualsTo.setRightExpression(new StringValue(value));
        dataPermissionSql = selfEqualsTo.toString();
​
        // dataPermissionSql = mainTableName + "." + key + " in ( " + CollUtil.join(new HashSet<>(), StringPool.COMMA) + " )";
​
        if (plainSelect.getWhere() == null) {
            plainSelect.setWhere(CCJSqlParserUtil.parseCondExpression(dataPermissionSql));
        } else {
            plainSelect.setWhere(new AndExpression(plainSelect.getWhere(),                        CCJSqlParserUtil.parseCondExpression(dataPermissionSql)));
        }
    }

3. mybatis拦截器的拦截范围 

Mybatis拦截器设计的初衷就是为了供用户在某些时候可以实现自己的逻辑而不必去动Mybatis固有的逻辑。通过Mybatis拦截器我们可以拦截某些方法的调用,我们可以选择在这些被拦截的方法执行前后加上某些逻辑,也可以在执行这些被拦截的方法时执行自己的逻辑而不再执行被拦截的方法。所以Mybatis拦截器的使用范围是非常广泛的。

       Mybatis里面的核心对象还是比较多,如下:

Mybatis拦截器并不是每个对象里面的方法都可以被拦截的。Mybatis拦截器只能拦截Executor、ParameterHandler、StatementHandler、ResultSetHandler四个对象里面的方法。

你可能感兴趣的:(mybatis,java)