Mybatis执行sql核心方法增删改Executor.update,查询ResultSetHandler.handleResultSets

1.Mybatis执行增删改的底层核心接口是Executor.update,如果我们想对它进行拦截处理,则需要拦截此方法

2.Mybatis执行查询的底层核心接口是ResultSetHandler.handleResultSets,如果我们想对它进行拦截处理,则需要拦截此方法

 

定义增删改的拦截器:

@Intercepts({
    @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
})
@Slf4j
public class IbatisUpdateInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        try {
            if (invocation.getTarget() instanceof Executor && invocation.getArgs().length == 2) {
                return invokeUpdate(invocation);
            }
        } finally {
            // 回滚到原生对象
            MybatisContextHolder.rollback();
        }

        return invocation.proceed();
    }

    private Object invokeUpdate(Invocation invocation) throws Exception {
        Executor executor = (Executor) invocation.getTarget();
        // 获取第一个参数
        MappedStatement ms = (MappedStatement) invocation.getArgs()[0];

        // 非insert/update,不处理
        if (ms.getSqlCommandType() != SqlCommandType.INSERT && ms.getSqlCommandType() != SqlCommandType.UPDATE) {
            return invocation.proceed();
        }

        Object paramObj = invocation.getArgs()[1];
        // 无参数不处理
        if (paramObj == null) {
            return invocation.proceed();
        }

        if (!needEncryptDecrypt(paramObj) && !isBatchUpdate(paramObj)) {
            return invocation.proceed();
        }

        // 加密
        if (paramObj instanceof Map) {
            Map map =
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值