From 28eb26d68f7a2a4e6b316c3ea9f511aa143db66e Mon Sep 17 00:00:00 2001
From: KKSU <15274802129@163.com>
Date: Mon, 30 Sep 2024 16:57:18 +0800
Subject: [PATCH] 55测试环境
---
src/main/java/com/xcong/excoin/configurations/interceptor/MybatisInterceptor.java | 96 +++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 95 insertions(+), 1 deletions(-)
diff --git a/src/main/java/com/xcong/excoin/configurations/interceptor/MybatisInterceptor.java b/src/main/java/com/xcong/excoin/configurations/interceptor/MybatisInterceptor.java
index 08aa177..5e69b7a 100644
--- a/src/main/java/com/xcong/excoin/configurations/interceptor/MybatisInterceptor.java
+++ b/src/main/java/com/xcong/excoin/configurations/interceptor/MybatisInterceptor.java
@@ -1,10 +1,104 @@
package com.xcong.excoin.configurations.interceptor;
+import com.xcong.excoin.common.LoginUserUtils;
+import com.xcong.excoin.common.contants.AppContants;
+import com.xcong.excoin.common.system.base.BaseEntity;
+import com.xcong.excoin.modules.member.entity.MemberEntity;
+import lombok.extern.slf4j.Slf4j;
+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.apache.ibatis.session.defaults.DefaultSqlSession;
+import org.springframework.stereotype.Component;
+
+import java.util.*;
+
/**
* mybatis拦截器,自动注入创建人、创建时间、修改人、修改时间
*
* @author wzy
* @date 2020-05-13
**/
-public class MybatisInterceptor {
+@Slf4j
+@Component
+@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
+public class MybatisInterceptor implements Interceptor {
+ @Override
+ public Object intercept(Invocation invocation) throws Throwable {
+ MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
+ SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
+
+ Object parameter = invocation.getArgs()[1];
+ if (parameter == null) {
+ return invocation.proceed();
+ }
+
+ if (SqlCommandType.INSERT == sqlCommandType) {
+ if (parameter instanceof DefaultSqlSession.StrictMap) {
+ Map map = (Map) parameter;
+ List list = (List) map.get("list");
+ for (Object o : list) {
+ injectForInsert(o);
+ }
+ } else {
+ injectForInsert(parameter);
+ }
+ }
+
+ if (SqlCommandType.UPDATE == sqlCommandType) {
+ if (parameter instanceof DefaultSqlSession.StrictMap) {
+ Map map = (Map) parameter;
+ List list = (List) map.get("list");
+ for (Object o : list) {
+ injectForUpdate(o);
+ }
+ } else {
+ injectForUpdate(parameter);
+ }
+ }
+
+ return invocation.proceed();
+ }
+
+ @Override
+ public Object plugin(Object o) {
+ return Plugin.wrap(o, this);
+ }
+
+ @Override
+ public void setProperties(Properties properties) {
+
+ }
+
+ public void injectForInsert(Object o) {
+ MemberEntity member = LoginUserUtils.getUser();
+ if (o instanceof BaseEntity) {
+ BaseEntity baseEntity = (BaseEntity) o;
+ if (member != null) {
+ String by = member.getPhone() != null ? member.getPhone() : member.getEmail();
+ baseEntity.setCreateBy(by);
+ baseEntity.setUpdateBy(by);
+ } else {
+ baseEntity.setCreateBy(AppContants.SYSTEM_USER);
+ baseEntity.setUpdateBy(AppContants.SYSTEM_USER);
+ }
+ baseEntity.setCreateTime(new Date());
+ baseEntity.setUpdateTime(new Date());
+ }
+ }
+
+ public void injectForUpdate(Object o) {
+ MemberEntity member = LoginUserUtils.getUser();
+ if (o instanceof BaseEntity) {
+ BaseEntity baseEntity = (BaseEntity) o;
+ if (member != null) {
+ String by = member.getPhone() != null ? member.getPhone() : member.getEmail();
+ baseEntity.setUpdateBy(by);
+ } else {
+ baseEntity.setUpdateBy(AppContants.SYSTEM_USER);
+ }
+ baseEntity.setUpdateTime(new Date());
+ }
+ }
}
--
Gitblit v1.9.1