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) {}
}
Mybatis 的插件, 主要处理 “创建人”、“创建时间”、“修改人”、“修改时间”
最新推荐文章于 2023-04-12 15:43:20 发布