Mybatis拦截器修改sql,获取传入参数

package com.interceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbgg.common.exception.BusinessException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.Select;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Properties;

@Component
@Intercepts({@Signature(
        type = Executor.class,
        method = "update",
        args = {MappedStatement.class, Object.class}
),@Signature(
        type = Executor.class,
        method = "query",
        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
), @Signature(
        type = Executor.class,
        method = "query",
        args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
)})
public class SplitTableInterceptor implements Interceptor {

    private final static String tableName = "tableName";

    public SplitTableInterceptor() {
    }

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement)args[0];
        Object parameter = args[1];
        BoundSql boundSql;
        //获取配置文件中最原始的sql
        boundSql = ms.getBoundSql(parameter);
        if(args.length>4){
            //其他拦截器处理后的sql
            boundSql = (BoundSql)args[5];
        }
        String sql = "select 1";

        Statement statement = CCJSqlParserUtil.parse(boundSql.getSql());
        if((statement instanceof Select) && args.length != 4){
            //查询
            boundSql = (BoundSql)args[5];
            Field field = boundSql.getClass().getDeclaredField("sql");
            field.setAccessible(true);
            field.set(boundSql, sql);
        }else {
            //增删改
            Field field = boundSql.getClass().getDeclaredField("sql");
            field.setAccessible(true);
            field.set(boundSql, sql);
            MappedStatement newStatement = newMappedStatement(ms, new BoundSqlSqlSource(boundSql));
            MetaObject msObject =  MetaObject.forObject(newStatement, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(),new DefaultReflectorFactory());
            msObject.setValue("sqlSource.boundSql.sql", sql);
            args[0] = newStatement;
        }
        return invocation.proceed();
    }

    private static MappedStatement newMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
        MappedStatement.Builder builder =
                new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType());
        builder.resource(ms.getResource());
        builder.fetchSize(ms.getFetchSize());
        builder.statementType(ms.getStatementType());
        builder.keyGenerator(ms.getKeyGenerator());
        if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
            StringBuilder keyProperties = new StringBuilder();
            for (String keyProperty : ms.getKeyProperties()) {
                keyProperties.append(keyProperty).append(",");
            }
            keyProperties.delete(keyProperties.length() - 1, keyProperties.length());
            builder.keyProperty(keyProperties.toString());
        }
        builder.timeout(ms.getTimeout());
        builder.parameterMap(ms.getParameterMap());
        builder.resultMaps(ms.getResultMaps());
        builder.resultSetType(ms.getResultSetType());
        builder.cache(ms.getCache());
        builder.flushCacheRequired(ms.isFlushCacheRequired());
        builder.useCache(ms.isUseCache());

        return builder.build();
    }


    @Override
    public Object plugin(Object o) {
        return Plugin.wrap(o, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
    //    定义一个内部辅助类,作用是包装sq
    static class BoundSqlSqlSource implements SqlSource {
        private BoundSql boundSql;
        public BoundSqlSqlSource(BoundSql boundSql) {
            this.boundSql = boundSql;
        }
        @Override
        public BoundSql getBoundSql(Object parameterObject) {
            return boundSql;
        }
    }
}

查询语句中是通过反射修改执行的sql实现,增删改语句中是通过替换MappedStatement

    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement)args[0];
        Object parameter = args[1];
        String sid = null;
        if(parameter instanceof Map){
            Map param = (Map) parameter;
            if(!param.containsKey("sid")){
                int size = param.keySet().size()/2;
                for(int i = 1; i<= size; i++){
                    String key = "param"+i;
                    Object o = param.get(key);
                    if(o instanceof QueryWrapper){
                        QueryWrapper q = (QueryWrapper) o;
                        String sqlSegment = q.getExpression().getSqlSegment();
                        String s = "sid =";
                        if(sqlSegment.contains(s)){
                            String pKey = sqlSegment.split(s)[1].split("paramNameValuePairs.")[1].split("}")[0];
                            sid = q.getParamNameValuePairs().get(pKey).toString();
                            break;
                        }
                    }
                }
            }else {
                sid = param.get("sid").toString();
            }
        }else if(parameter instanceof String){
                sid = parameter.toString();
        }else{
            JSONObject json = (JSONObject) JSON.toJSON(parameter);
            sid = json.get("sid").toString();
        }
        if(sid == null){
            throw new Exception("获取信息失败");
        }
        System.out.println("sid : "+sid)
        return invocation.proceed();
    }

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis 中,可以通过拦截器(Interceptor)来对 SQL 执行过程进行干预和增强。如果你想在拦截器中添加额外的参数,可以按照以下步骤进行操作: 1. 创建一个实现了 `Interceptor` 接口的拦截器类,并实现其中的方法。最常用的方法是 `intercept`,该方法用于拦截 SQL 执行过程。 2. 在 `intercept` 方法中,你可以通过 `Invocation` 对象获取到当前执行的 SQL 相关信息,例如参数、Statement 对象等。 3. 在拦截器中添加额外的参数,可以通过以下方式实现: - 创建一个新的参数对象,将需要传递的额外参数设置到该对象中。 - 调用 `Invocation` 对象的 `getArgs` 方法获取到方法的参数数组。 - 将新的参数对象添加到参数数组中,可以使用 `Arrays.copyOf` 方法来创建一个新的数组,并将新参数对象添加到数组末尾。 - 调用 `Invocation` 对象的 `proceed` 方法,传入修改后的参数数组,继续执行 SQL 语句。 4. 在 MyBatis 配置文件中配置拦截器。将自定义的拦截器添加到 `<plugins>` 标签中,并指定拦截的目标方法或语句。 下面是一个简单的示例代码,演示了如何在拦截器中添加额外参数: ```java @Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) }) public class MyInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { // 获取参数 Object[] args = invocation.getArgs(); Connection connection = (Connection) args[0]; Integer transactionTimeout = (Integer) args[1]; // 添加额外参数 MyExtraParam extraParam = new MyExtraParam(); // 设置额外参数值 extraParam.setProperty("key", "value"); // 将新的参数对象添加到参数数组中 Object[] newArgs = Arrays.copyOf(args, args.length + 1); newArgs[args.length] = extraParam; // 修改参数并继续执行 SQL 语句 args[0] = connection; args[1] = transactionTimeout; invocation.proceed(); return null; } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { // 设置拦截器的属性 } } ``` 注意,以上示例代码仅为演示目的,并未完整实现拦截器的功能。你可以根据自己的需求进行修改和扩展。同时,需要在 MyBatis 配置文件中配置该拦截器,使其生效。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值