MyBatis 插件之拦截器(Interceptor)

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

//项目实际使用  就是在你进行数据库操作时,进行数据的第二次封装

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
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;
    }
}

  

posted @   好记性不如烂笔头=>  阅读(2278)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示