Mybatis 的插件, 主要处理 “创建人”、“创建时间”、“修改人”、“修改时间”

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @ClassName: BaseDataInterceptor.java
 * @Description: Mybatis 的插件, 主要处理 “创建人”、“创建时间”、“修改人”、“修改时间”。 
 * 注意: 版本号需要自己来维护
 *
 * @version: v1.0.0
 * @author: 
 * @date: 2019年5月28日 
 */
@Intercepts(
		{
			@Signature(type = Executor.class, method = ApplicationConstant.UPDATE, args = {MappedStatement.class, Object.class})
		}
)
@Slf4j
@SuppressWarnings("all")
public class BaseDataInterceptor implements Interceptor {
	
	private static final Long VERSION  = 1L;
	
	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
		SqlCommandType commandType = ms.getSqlCommandType();
		Object parameter = invocation.getArgs()[1];
		String methodName = invocation.getMethod().getName();
		log.info("【NormalPlugin, ==================> SqlCommandType:[{}]】", commandType);
	    String createOpId = null;
	    String lastMntOpId = null;
	    Date currDate = new Date();
		if (parameter instanceof BaseDomain) {
			BaseDomain entity = (BaseDomain) parameter;
			if (entity != null) {
				if (StringUtils.isNotBlank(entity.getCreateOpId())) {
					createOpId = entity.getCreateOpId();
				} else {
					createOpId = ApplicationConstant.DEFAULT_CREATE_OPID;
				}
				if (StringUtils.isNotBlank(entity.getLastMntOpId())) {
					lastMntOpId = entity.getCreateOpId();
				} else {
					lastMntOpId = ApplicationConstant.DEFAULT_CREATE_OPID;
				}
			}
			if (StringUtils.isNotBlank(methodName) && ApplicationConstant.UPDATE.equals(methodName)) {
				if (commandType.equals(SqlCommandType.INSERT)) {
					entity.setCreateOpId(createOpId);
					entity.setCreateTs(currDate);
					entity.setVersionCt(VERSION);
				} else if (commandType.equals(SqlCommandType.UPDATE)) {
					entity.setLastMntOpId(lastMntOpId);
					entity.setLastMntTs(currDate);
				}
			}
		} else if (parameter instanceof Map) {
			if (commandType.equals(SqlCommandType.UPDATE)) {
				Map map = (Map) parameter;
				if (map.containsKey(ApplicationConstant.CREATE_OPID_FIELD)) {
					map.put(ApplicationConstant.CREATE_TS_FIELD, currDate);
				}
				if (map.containsKey(ApplicationConstant.LASTMNT_OPID_FIELD)) {
					map.put(ApplicationConstant.LASTMNT_TS_FIELD, currDate);
				}
			} else if (commandType.equals(SqlCommandType.INSERT)) {
				Iterator<Entry<String, Object>> entries = ((Map) parameter).entrySet().iterator();
				while(entries.hasNext()){
				    Entry<String, Object> entry = entries.next();
				    Object obj = entry.getValue();
				    log.info("【NormalPlugin, ==================> domainStr:[{}], value:[{}]】", entry.getKey(), obj);
				    if ( obj instanceof List) {
				    	List<Object> list = (List) obj;
				    	for (Object ob :  list) {
				    		if ( ob instanceof BaseDomain) {
								BaseDomain entity = (BaseDomain) ob;
								if (entity != null) {
									if (StringUtils.isNotBlank(entity.getCreateOpId())) {
										createOpId = entity.getCreateOpId();
									} else {
										createOpId = ApplicationConstant.DEFAULT_CREATE_OPID;
									}
									if (StringUtils.isNotBlank(entity.getLastMntOpId())) {
										lastMntOpId = entity.getCreateOpId();
									} else {
										lastMntOpId = ApplicationConstant.DEFAULT_CREATE_OPID;
									}
								}
								entity.setCreateOpId(createOpId);
								entity.setCreateTs(currDate);
								entity.setVersionCt(VERSION);
							}
				    	}
				    }
				}
			}
		}
		return invocation.proceed();
	}

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

	@Override
	public void setProperties(Properties properties) {}

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值