Mybatis 拦截器 ---操作人拦截器


@Slf4j
@Intercepts({
        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
})
public class OperatorInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        String name = invocation.getMethod().getName();
        log.debug("Exec MethodName is {}", name);

        SqlCommandType sqlCommandType = null;

        //遍历处理所有参数,update方法有两个参数,参见Executor类中的update()方法。
        for (int i = 0; i < args.length; i++) {
            Object arg = args[i];

            // -- 第一个参数处理。根据它判断是否给“操作属性”赋值。
            if (arg instanceof MappedStatement) {
                MappedStatement ms = (MappedStatement) arg;
                sqlCommandType = ms.getSqlCommandType();

                // 如果是“增加”或“更新”操作,则继续进行默认操作信息赋值。否则,则退出
                if (sqlCommandType == SqlCommandType.INSERT || sqlCommandType == SqlCommandType.UPDATE) {
                    continue;
                } else {
                    break;
                }
            }

            // -- 第二个参数处理。实际的请求参数

            // 如果是map,有两种情况:(1)使用@Param多参数传入,由Mybatis包装成map。(2)原始传入Map
            if (arg instanceof Map) {
                Map map = (Map) arg;
                putProperty(map, sqlCommandType);

                for (Object value : map.values()) {
                    if (value instanceof Collection) {
                        for (Object e : ((Collection) value)) {
                            setProperty(e, sqlCommandType);
                        }
                    } else {
                        setProperty(value, sqlCommandType);
                    }
                }

                //原始参数传入
            } else {
                setProperty(arg, sqlCommandType);
            }
        }
        return invocation.proceed();
    }

    private void putProperty(Map map, SqlCommandType sqlCommandType) {
        try {
            String userName = SecurityUtils.getUserName();
            if (SqlCommandType.INSERT == sqlCommandType) {
                map.putIfAbsent("sysCreatedBy", userName);
                map.putIfAbsent("sysCreatedDate", TimeUtil.nowLocalDateTime());
                map.putIfAbsent("sysLastModifiedBy", null);
                map.putIfAbsent("sysLastModifiedDate", null);
            } else if (SqlCommandType.UPDATE == sqlCommandType) {
                map.putIfAbsent("sysCreatedBy", null);
                map.putIfAbsent("sysCreatedDate", null);
                map.putIfAbsent("sysLastModifiedBy", userName);
                map.putIfAbsent("sysLastModifiedDate", TimeUtil.nowLocalDateTime());
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    private void setProperty(Object obj, SqlCommandType sqlCommandType) {
        //空对象返回
        if(ObjectUtils.isEmpty(obj)){
            return;
        }
        try {
            String userName = SecurityUtils.getUserName();
            if (SqlCommandType.INSERT == sqlCommandType) {
                BeanUtils.setProperty(obj, "sysCreatedBy", userName);
                BeanUtils.setProperty(obj, "sysCreatedDate", LocalDateTime.now());
                BeanUtils.setProperty(obj, "sysLastModifiedBy", null);
                BeanUtils.setProperty(obj, "sysLastModifiedDate", null);
            } else if (SqlCommandType.UPDATE == sqlCommandType) {
                BeanUtils.setProperty(obj, "sysCreatedBy", null);
                BeanUtils.setProperty(obj, "sysCreatedDate", null);
                BeanUtils.setProperty(obj, "sysLastModifiedBy", userName);
                BeanUtils.setProperty(obj, "sysLastModifiedDate", TimeUtil.nowLocalDateTime());
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

    }

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

    }

    @Override
    public void setProperties(Properties properties) {
    }
}

 注入到Spring中

 // 操作信息拦截器
    @Bean
    public OperatorInterceptor operatorInterceptor() {
        return new OperatorInterceptor();
    }

 

 

你可能感兴趣的:(Mybatis 拦截器 ---操作人拦截器)