Java mybatis拦截器,重装sql,笔记

1.实现Interceptor拦截器,重写拦截器方法,实现自己的改装

@Intercepts({
    @Signature(
    type= Executor.class,
    method = "query",
    args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
    )
})
@Component
public class ExecutorInterceptor implements Interceptor {
@SuppressWarnings("rawtypes")
@Override
public Object intercept(Invocation invocation) throws Throwable {
    
    Object[] args = invocation.getArgs();
    MappedStatement ms = (MappedStatement) args[0];
    Object parameterObject = args[1];
    RowBounds rowBounds = (RowBounds) args[2];
    ResultHandler resultHandler = (ResultHandler) args[3];
    Executor executor = (Executor) invocation.getTarget();
    BoundSql boundSql = ms.getBoundSql(parameterObject);
    String sql = boundSql.getSql();
    System.out.println("拦截器ExecutorInterceptor:"+sql);
    if(sql.toUpperCase().indexOf("WHERE")>=0) {
    	sql = sql.replace("WHERE", "WHERE t.id = '1' AND");
    	
    	System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>" + sql);
    }
    
    CacheKey cacheKey = executor.createCacheKey(ms, parameterObject, rowBounds, boundSql);
// 调用其本身的传6参的方法执行改装后的sql
    return executor.query(ms, parameterObject, rowBounds, resultHandler, cacheKey, boundSql);

}

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

@Override
public void setProperties(Properties properties) {

}
}

2.借鉴网上的方法 链接地址:https://blog.csdn.net/cowbin2012/article/details/85256360

代码整理贴出

package com.szhz.util;

import java.sql.SQLException;
import java.util.Properties;

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.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
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;

@Intercepts({
    @Signature(
    type= Executor.class,
    method = "query",
    args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
    )
})
@Component
public class ExecutorInterceptor implements Interceptor {
@SuppressWarnings("rawtypes")
@Override
public Object intercept(Invocation invocation) throws Throwable {
    String sql = ExecutorInterceptor.getSqlByInvocation(invocation);
    //可以对sql重写
    System.out.println("拦截器ExecutorInterceptor:"+sql);
    if(sql.toUpperCase().indexOf("WHERE")>=0) {
    	sql = sql.replace("WHERE", "WHERE t.group_id = '1' AND t.manage_id = '1' AND");
    	
    	System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>" + sql);
    }
      ExecutorInterceptor.resetSql2Invocation( invocation,  sql);
    
     return invocation.proceed();
    
    

}

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

@Override
public void setProperties(Properties properties) {

}


/**
 * 获取sql语句
 * @param invocation
 * @return
 */
public static String getSqlByInvocation(Invocation invocation) {
    final Object[] args = invocation.getArgs();
    MappedStatement ms = (MappedStatement) args[0];
    Object parameterObject = args[1];
    BoundSql boundSql = ms.getBoundSql(parameterObject);
    return boundSql.getSql();
}

/**
 * 包装sql后,重置到invocation中
 * @param invocation
 * @param sql
 * @throws SQLException
 */
public static void resetSql2Invocation(Invocation invocation, String sql) throws SQLException {
    final Object[] args = invocation.getArgs();
    MappedStatement statement = (MappedStatement) args[0];
    Object parameterObject = args[1];
    BoundSql boundSql = statement.getBoundSql(parameterObject);
    MappedStatement newStatement = newMappedStatement(statement, new BoundSqlSqlSource(boundSql));
    MetaObject msObject =  MetaObject.forObject(newStatement, new DefaultObjectFactory(),
            new DefaultObjectWrapperFactory(),new DefaultReflectorFactory());
    msObject.setValue("sqlSource.boundSql.sql", sql);
    args[0] = newStatement;
}
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();
}


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




}

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值