mybatis拦截器

拦截器类: 

package com.jiahe.sgjcfx.handler;

import cn.hutool.core.util.ReflectUtil;
import com.jiahe.sgjcfx.annotation.DifferentHandler;
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.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

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


@Intercepts({
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
//        @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})
})
public class HandlerDiffInterceptor implements Interceptor {

    private static final Integer MAPPED_STATEMENT_INDEX = 0;
    private static final Integer PARAM_OBJ_INDEX = 1;
    private static final Integer BOUND_SQL_INDEX = 5;
    private static final String DIFF_ONE = "11";
    private static final String DIFF_ONE_SON = "('22','33','44')";
    private static final String DIFF_TWO = "55";
    private static final String DIFF_TWO_SON = "('66','77','88')";

    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement) args[MAPPED_STATEMENT_INDEX];
        // 根据全限定名判断mapper类上有没有注解
        String msId = ms.getId();
        msId = msId.substring(0, msId.lastIndexOf("."));
        Class<?> mapperClass = Class.forName(msId);
        boolean present = mapperClass.isAnnotationPresent(DifferentQXHandler.class);
        if (present) {
            Object parameter = args[PARAM_OBJ_INDEX];
            BoundSql boundSql = args.length == 4 ? ms.getBoundSql(parameter) : (BoundSql) args[BOUND_SQL_INDEX];

            Object obj = boundSql.getParameterObject();
            String sql = boundSql.getSql();
            if (sql.trim().toUpperCase().startsWith("SELECT")) {
                Field qxdm = obj.getClass().getDeclaredField("qxdm");
                qxdm.setAccessible(true);
                String value = (String) qxdm.get(obj);
                if (DIFF_ONE.equals(value)) {
                    ReflectUtil.setFieldValue(obj, "qxdm", DIFF_ONE_SON);
                } else if (DIFF_TWO.equals(value)) {
                    ReflectUtil.setFieldValue(obj, "qxdm", DIFF_TWO_SON);
                }

                sql = sql.replace("CODE =", "CODE in");

                ReflectUtil.setFieldValue(boundSql, "sql", sql);

            }
        }
        return invocation.proceed();
    }


    @Override
    public Object plugin(Object target) {
        return Interceptor.super.plugin(target);
    }

    @Override
    public void setProperties(Properties properties) {
        Interceptor.super.setProperties(properties);
    }

}

注解类:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DifferentHandler {

}

 配置类:

@Bean
    public String myInterceptor(SqlSessionFactory sqlSessionFactory) {
        //实例化插件
        HandlerDiffInterceptor sqlInterceptor = new HandlerDiffInterceptor();
        //创建属性值
        Properties properties = new Properties();
        properties.setProperty("prop1","value1");
        //将属性值设置到插件中
        sqlInterceptor.setProperties(properties);
        //将插件添加到SqlSessionFactory工厂
        sqlSessionFactory.getConfiguration().addInterceptor(sqlInterceptor);
        return "interceptor";
    }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值