1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
import cn.hutool.core.lang.Validator; import cn.z201.quick.dev.app.utils.SnowflakeTool; import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; import com.baomidou.mybatisplus.core.toolkit.EncryptUtils; import com.baomidou.mybatisplus.core.toolkit.PluginUtils; import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlCommandType; import org.apache.ibatis.plugin.*; import org.springframework.stereotype.Component;
import java.sql.Connection; import java.time.Clock; import java.util.*;
@Slf4j @Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }), }) @Component // 可以直接在插件上增加Component public class MybatisInterceptor implements Interceptor {
@Override public Object intercept(Invocation invocation) throws Throwable { Long startTime = Clock.systemDefaultZone().millis(); try { Object[] args = invocation.getArgs(); MappedStatement mappedStatement = (MappedStatement) args[0]; Object entity = args[1]; if (SqlCommandType.INSERT.name().equalsIgnoreCase(mappedStatement.getSqlCommandType().name())) { List<Object> entitySet = getEntitySet(entity); for (Object object : entitySet) { insertProcess(object); } } else if (SqlCommandType.UPDATE.name().equalsIgnoreCase(mappedStatement.getSqlCommandType().name())) { List<Object> entitySet = getEntitySet(entity); for (Object object : entitySet) { updateProcess(object); } } return invocation.proceed(); } finally { Long timeConsuming = Clock.systemDefaultZone().millis() - startTime; log.info("SQL RunTime {} ms", timeConsuming); } }
private List<Object> getEntitySet(Object object) { List<Object> set = new ArrayList<>(); if (object instanceof Map) { Iterator entries = ((Map<?, ?>) object).entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = (Map.Entry) entries.next(); Object value = entry.getValue(); if (value instanceof Collection) { set.addAll((Collection<?>) value); } else { set.add(value); } } } else { set.add(object); } return set; }
private void insertProcess(Object object) { Long time = Clock.systemDefaultZone().millis(); if (object instanceof BaseEntity) { BaseEntity baseEntity = (BaseEntity) object; baseEntity.setId(SnowflakeTool.getInstance().nextId()); if (Validator.isNull(baseEntity.getIsEnable())) { baseEntity.setIsEnable(true); } if (Validator.isNull(baseEntity.getCreateTime())) { baseEntity.setCreateTime(time); } if (Validator.isNull(baseEntity.getUpdateTime())) { baseEntity.setUpdateTime(time); } } }
private void updateProcess(Object object) { Long time = Clock.systemDefaultZone().millis(); if (object instanceof BaseEntity) { BaseEntity baseEntity = (BaseEntity) object; if (Validator.isNull(baseEntity.getUpdateTime())) { baseEntity.setUpdateTime(time); } } }
}
|