• 大小: 7KB
    文件类型: .java
    金币: 1
    下载: 0 次
    发布日期: 2021-05-05
  • 语言: Java
  • 标签: mybati  interceptor  

资源简介

通过mybatis拦截器将查询语句、更新语句、删除语句、插入语句中指定表明替换为另一个表名

资源截图

代码片段和文件信息

package com.panpass.marketing.activity.config.web;

import com.panpass.marketing.activity.common.util.ContextUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.statement.baseStatementHandler;
import org.apache.ibatis.executor.statement.RoutingStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.*;
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.scripting.xmltags.DynamicContext;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.tools.ant.util.ReflectUtil;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

@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 Myinterceptor1  implements Interceptor {

    private  static  ArrayList tables =new ArrayList();
    static {
        tables.add(“ACT_CONFIG“);
    }

    @Override
    public object intercept(Invocation invocation) throws Throwable {
         object[] args = invocation.getArgs();
         if(args.length==2){//更新
             return  exectorUpdate(invocation);
         }else{
             return  exectorQuery(invocation);
         }
    }

   private  object  exectorUpdate(Invocation invocation) throws SQLException{
       Executor executor = (Executor) invocation.getTarget();
       object[] args = invocation.getArgs();
       MappedStatement ms = (MappedStatement) args[0];
       object parameter = args[1];
       BoundSql boundSql =  ms.getBoundSql(parameter);
       String  sql =getSql(boundSql.getSql());
       MyBoundSql  myBoundSql =new MyBoundSql(ms.getConfiguration()sqlboundSql.getParameterMappings()boundSql.getParameterobject());
       additionalParameters(boundSqlmyBoundSql);

       StatementHandler handler = new RoutingStatementHandler(executormsparameterRowBounds.DEFAULTnullmyBoundSql);
       Statement statement =  handler.prepare(ms.getConfiguration().getEnvironment().getDa

评论

共有 条评论