Mybatis拦截器处理DML拦截demo

package cn.hydee.rap.base.aspect;


import cn.hydee.rap.base.dto.LoginUserDTO;
import cn.hydee.rap.base.exception.BaseException;
import cn.hydee.rap.base.exception.BizException;
import cn.hydee.rap.base.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;

import java.util.Properties;

@Slf4j
@Component
@Intercepts({
        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
//        ,
//        @Signature(method = "query", type = Executor.class, args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class})
        })
@SuppressWarnings("unchecked")

public class JDBCInterceptor  implements Interceptor {



    @Override
    public Object intercept(Invocation invocation) throws BaseException, InvocationTargetException, IllegalAccessException {
        log.info("进入DML操作拦截器!");

      LoginUserDTO loginUserDTO =UserUtils.getLoginUser();
        if(loginUserDTO!=null&& loginUserDTO.getIsDrugSupervisor()==1){
            log.error("药监无DML权限!");
            throw new BizException("无DML操作权限!");
        }

        return invocation.proceed();
    }
    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target,this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}

你可能感兴趣的:(Mybatis拦截器处理DML拦截demo)