码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis 插件之拦截器(Interceptor)

时间:2019-10-14 13:07:06      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:update   edm   static   tail   statement   pre   logger   RoCE   row   

参考 https://blog.csdn.net/weixin_39494923/article/details/91534658

//项目实际使用

package com.yueworldframework.core.mybatis;

import com.yueworldframework.core.support.SessionHelper;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Properties;

/**
 * Created by wangbs
 */
@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
public class AuditingInterceptor implements Interceptor {
    private static final Logger logger = LoggerFactory.getLogger(AuditingInterceptor.class);

    private Properties props = null;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
        if (invocation.getArgs()[1] instanceof Pojo) {
            Pojo parameter = (Pojo) invocation.getArgs()[1];
            if (SqlCommandType.INSERT == sqlCommandType) {
                // 初始化主键ID
                Method initializeUUID = BeanUtils.findDeclaredMethod(parameter.getClass(), "initializeUUID");
                if (null != initializeUUID) {
                    initializeUUID.invoke(parameter);
                }
                if(null==parameter.getCreator()){
                    parameter.setCreator(SessionHelper.getId());
                }
                parameter.setUpdater(SessionHelper.getId());
                parameter.setCreatedDate(new Date());
                parameter.setUpdatedDate(parameter.getCreatedDate());
                parameter.setVersion(1);
            } else if (SqlCommandType.UPDATE == sqlCommandType) {
                parameter.setUpdater(SessionHelper.getId());
                parameter.setUpdatedDate(new Date());
                parameter.setVersion(parameter.getVersion() + 1);
            }
        }
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        if (target instanceof Executor) {
            return Plugin.wrap(target, this);
        } else {
            return target;
        }
    }

    public void setProperties(Properties properties) {
        if (null != properties && !properties.isEmpty()) props = properties;
    }
}

  

MyBatis 插件之拦截器(Interceptor)

标签:update   edm   static   tail   statement   pre   logger   RoCE   row   

原文地址:https://www.cnblogs.com/xiaowangbangzhu/p/11670657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!