zoukankan      html  css  js  c++  java
  • mybatis拦截器对SQL处理,数据权限逻辑控制

    import org.apache.ibatis.cache.CacheKey;
    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.SqlCommandType;
    import org.apache.ibatis.plugin.*;
    import org.apache.ibatis.session.ResultHandler;
    import org.apache.ibatis.session.RowBounds;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.lang.reflect.Method;
    import java.sql.Connection;
    import java.util.Properties;
    
    @Intercepts(
            {
                    @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.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 SqlContextInterceptor implements Interceptor {
    
        private final Logger logger = LoggerFactory.getLogger(SqlContextInterceptor.class);
    
        private Properties properties;
    
        @Override
        public Object intercept(Invocation invocation) throws Throwable {
    
            Executor executor = (Executor) invocation.getTarget();
    
            Method invocationMethod = invocation.getMethod();
    
            Object[] invocationArgs = invocation.getArgs();
    
            MappedStatement mappedStatement = (MappedStatement) invocationArgs[0];
    
            SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
    
            if (!SqlCommandType.SELECT.equals(sqlCommandType)) {
                return invocation.proceed();
            }
    
            Object parameter = invocationArgs[1];
            RowBounds rowBounds = (RowBounds) invocationArgs[2];
            ResultHandler resultHandler = (ResultHandler) invocationArgs[3];
    
            CacheKey cacheKey;
            BoundSql boundSql;
    
            //由于逻辑关系,只会进入一次
            if (invocationArgs.length == 4) {
                //4 个参数时
                boundSql = mappedStatement.getBoundSql(parameter);
                cacheKey = executor.createCacheKey(mappedStatement, parameter, rowBounds, boundSql);
            } else {
                //6 个参数时
                cacheKey = (CacheKey) invocationArgs[4];
                boundSql = (BoundSql) invocationArgs[5];
            }
    
            // 在这里增加数据权限逻辑
    
            return executor.query(mappedStatement, parameter, rowBounds, resultHandler, cacheKey, boundSql);
        }
    
        @Override
        public Object plugin(Object target) {
            if (target instanceof Executor) {
                return Plugin.wrap(target, this);
            }
            return target;
        }
    
        @Override
        public void setProperties(Properties properties) {
            this.properties = properties;
            this.logger.info(this.properties.toString());
        }
    }
  • 相关阅读:
    解决Xcode8打印了nw_socket_handle_socket_event Event mask
    调用系统框架使用设备系统语言的设置,相册相机设置为中文
    ios开发 之 设置多种文字颜色/背景色/文字下划线/行间距 NSString
    IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法
    UITableView设置cell的separator 分割线
    iOS用户点击推送消息进入应用后自动跳转到对应的ViewController
    随感
    JS获取当前网页大小以及屏幕分辨率等
    js将秒转换为 分:秒 函数
    css实现强制不换行/自动换行/强制换行
  • 原文地址:https://www.cnblogs.com/se7end/p/14122938.html
Copyright © 2011-2022 走看看