天天看點

SpringBoot2整合Mybatis攔截器,攔截mapper接口的某個方法

需求:

  在執行某個動态sql時,where 子句,希望通過使用者進行自定義查詢條件,比如使用者可以傳入 “id > 100011 and name = '張三'” 的多條件表達式進行查詢 (注意:這裡的條件查詢,為了安全性的考慮,是經過處理的,以免出現安全漏洞。)

方法:

       1. 自定義mybatis的攔截器MySqlInterceptor(繼承 org.apache.ibatis.plugin.Interceptor)對執行的mapper接口進行攔截

       2. 改寫對應 intercept()方法。

       3. 添加到項目檔案中,進行攔截器配置。

以下代碼僅做參考學習使用。

自定義攔截器代碼如下:

package com.hlyjy.zj.interceptor;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
 * @author junzhou
 * @date 2021/1/5 13:55
 * @description: 自定義 MyBatis 攔截器
 * 用于攔截 mapper 接口中的動态查詢語句,隻對 mapper 接口中的 searchByQuery 方法啟作用
 * @since 1.8
 */
@Intercepts({@Signature(type = Executor.class, method = "query",
        args = {MappedStatement.class,
                Object.class,
                RowBounds.class,
                ResultHandler.class})})
public class MySqlInterceptor implements Interceptor {

    /**
     * 日志記錄器
     */
    private static final Logger logger= LoggerFactory.getLogger(MySqlInterceptor.class);

    /**
     * intercept 方法用來對攔截的 sql 進行具體的操作
     * 本攔截方法隻對 mapper 接口中的 searchByQuery 方法進行攔截,實際每個方法都攔截了,
     * 隻是隻有 searchByQuery 方法時,才真正執行 攔截的相關操作
     * @param invocation 攔截器執行器
     * @return
     * @throws Throwable 異常資訊
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        //  logger.info("執行intercept方法:{}", invocation.toString());
        //  擷取 invocation 傳遞的參數
        Object[] args = invocation.getArgs();

        MappedStatement ms = (MappedStatement) args[0];
        // 擷取執行的該攔截器的全路徑方法 比如你的 UserInfoMapper 接口的 getById 方法, com.xx.UserInfoMapper.getById
        String id = ms.getId();
        // 如果不是 searchByQuery 方法,就不進行相關的攔截操作
        if (!filterMethodById(id)){
            return invocation.proceed();
        }
        // 該參數類型 org.apache.ibatis.binding.MapperMethod$ParamMap
        Object parameterObject = args[1];

        // 擷取傳遞的參數, 主要包含三個參數 1. 查詢語句 searchQuery, 2. 查詢偏移 pageOffset, 3. 查詢每頁的資料條數
        MapperMethod.ParamMap paramMap = (MapperMethod.ParamMap) parameterObject;

        // 對查詢的條件進行重新拼接
        // searchQuery 查詢的條件
        String searchQuery = "";
        // pageOffset 分頁查詢的偏移
        int pageOffset = 0 ;
        // pageSize 分頁查詢頁面大小,即每頁多少條資料
        int pageSize = 10;
        // 取出各個參數并指派
        if (paramMap.containsKey("searchQuery")){
            searchQuery = (String) paramMap.get("searchQuery");
        }
        if (paramMap.containsKey("pageOffset")){
            pageOffset = (int) paramMap.get("pageOffset");
        }
        if (paramMap.containsKey("pageSize")){
            pageSize = (int) paramMap.get("pageSize");
        }

        BoundSql boundSql = ms.getBoundSql(parameterObject);
        // 擷取原始查詢的 sql 語句
        String origSql = boundSql.getSql();
        logger.error("原始SQL: {}", origSql);
        // 建構新的 sql 語句, 将 where 條件和 limit 條件加入
        String newSql = null;
        // 如果存在條件查詢則 拼接條件,如果不存在,則隻添加分頁限制
        if (!searchQuery.equals("")){
             newSql =  origSql +" WHERE " + searchQuery + " LIMIT " + pageSize + " OFFSET " + pageOffset;
        }else {
            newSql = origSql + " LIMIT " + pageSize + " OFFSET " + pageOffset;
        }

        // 重新new一個查詢語句對象
        BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), newSql,
                boundSql.getParameterMappings(), boundSql.getParameterObject());

        // 把新的查詢放到 statement 裡
        MappedStatement newMs = newMappedStatement(ms, new MySqlInterceptor.BoundSqlSqlSource(newBoundSql));
        for (ParameterMapping mapping : boundSql.getParameterMappings()) {
            String prop = mapping.getProperty();
            if (boundSql.hasAdditionalParameter(prop)) {
                newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
            }
        }

        // 修改 MappedStatement
        Object[] queryArgs = invocation.getArgs();
        queryArgs[0] = newMs;
        logger.info("攔截了 " + id + " 相關執行。");
        return invocation.proceed();
    }

    /**
     * 定義一個内部輔助類,作用是包裝 SQL
     */
    class BoundSqlSqlSource implements SqlSource {
        private BoundSql boundSql;
        public BoundSqlSqlSource(BoundSql boundSql) {
            this.boundSql = boundSql;
        }
        @Override
        public BoundSql getBoundSql(Object parameterObject) {
            return boundSql;
        }

    }

    /**
     * 根據擷取到執行 id 找到對應的方法,隻在 searchByQuery 方法上執行攔截
     * @param id 根據 MappedStatement 擷取到的 id 屬性
     * @return 是否是 searchByQuery 方法
     */
    private boolean filterMethodById(String id){
        System.out.println("id: " + id);
        String[] names = id.split("\\.");
        System.out.println("names: " + names.length);
        return names[names.length - 1].equals("searchByQuery");
    }


    private 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) {
            builder.keyProperty(ms.getKeyProperties()[0]);
        }
        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();
    }

    /***
     * 定義攔截的類 Executor、ParameterHandler、StatementHandler、ResultSetHandler當中的一個
     * @param target 需要攔截的類
     * @return
     */
    @Override
    public Object plugin(Object target) {
        if (target instanceof Executor) {
            return Plugin.wrap(target, this);
        }
        return target;

    }

    /**
     * 屬性相關操作
     * 設定和自定義屬性值
     * @param properties 屬性值
     */
    @Override
    public void setProperties(Properties properties) {
        // 擷取屬性
        // String value1 = properties.getProperty("prop1");
    }

}
           

配置類:

package com.hlyjy.zj.config;

import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;
import com.hlyjy.hbase.Interceptor.MySqlInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Properties;

@Configuration
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
public class MyBatisConfig {

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @PostConstruct
    public void addMySqlInterceptor() {
        MySqlInterceptor interceptor = new MySqlInterceptor();
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(interceptor);

        }
    }

}
           

在resource目錄下建立META-INF目錄,然後添加spring.factories檔案,其中内容如下:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration,\
com.hlyjy.zj.config.MyBatisConfig  # 其中 com.hlyjy.zj.config 為你自己配置類的包名,MyBatisConfig 為我的配置類名 
           

如有不足之處,歡迎大家交流學習。

繼續閱讀