开始摆烂-Mybatis之拦截器自动添加创建人创建时间更新人更新时间

痛点

在实际开发过程中:在inset和update过程方法中要不停的set的字段,是不是看着就感觉恶心(对代码有洁癖,对代码有追求的人)包括我也是

然而今天我就写了这种代码 摆烂自己

		entity.setCreateBy("sss");
        entity.setCreateTime(new Date());
        entity.setUpdateBy("ssss");
        entity.setUpdateTime(new Date());

振作起来~~

另外个同事就开骂,这人代码写的真有水平。哈哈哈

开始我的表演

源码地址

源码目录:com.song.kkxxpoi.common.AutoInsertTimeAndUser

在这里插入图片描述

开始摆烂-Mybatis之拦截器自动添加创建人创建时间更新人更新时间·视频

代码截图

web

在这里插入图片描述

Interceptor-拦截器

/**
 * 功能描述:拦截器  反射
 *帮助文档:https://blog.csdn.net/minghao0508/article/details/124420953
 * @author Songxianyang
 * @date 2022-08-12 21:50
 */
@Component
@Intercepts({@Signature(type = Executor.class, method = "update",
        args = {MappedStatement.class, Object.class})})
public class AutoInsertTimeAndUser implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        // 线程安全
        AtomicReference<User> userAtomicReference = new AtomicReference<>();
        userAtomicReference.set(new User("sxy"));
        
        // 获取一个MappedStatement
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        // 获取 SQL 命令 UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH
        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
        // 获取参数
        Object parameter = invocation.getArgs()[1];
    
        // 获取私有成员变量
        Field[] declaredFields = parameter.getClass().getDeclaredFields();
    
        for (Field field : declaredFields) {
            if (Objects.nonNull(field.getAnnotation(CreateTime.class))) {
                // 插入
                if (SqlCommandType.INSERT.equals(sqlCommandType)) {
                    field.setAccessible(true);
                    field.set(parameter, new Date());
                }
            }
            if (Objects.nonNull(field.getAnnotation(CreateUser.class))) {
                // 插入
                if (SqlCommandType.INSERT.equals(sqlCommandType)) {
                    field.setAccessible(true);
                    field.set(parameter, userAtomicReference.get().getName());
                }
            }
            if (Objects.nonNull(field.getAnnotation(UpdateTime.class))) {
                // 修改
                if (SqlCommandType.UPDATE.equals(sqlCommandType)|| SqlCommandType.INSERT.equals(sqlCommandType)) {
                    field.setAccessible(true);
                    field.set(parameter, new Date());
                }
            }
            if (Objects.nonNull(field.getAnnotation(UpdateUser.class))) {
                // 修改
                if (SqlCommandType.UPDATE.equals(sqlCommandType)||SqlCommandType.INSERT.equals(sqlCommandType)) {
                    field.setAccessible(true);
                    field.set(parameter, userAtomicReference.get().getName());
                }
            }
        }
        
        //获取方法参数
        Object[] args = invocation.getArgs();
        //获取方法名
        invocation.getMethod();
        //获取代理对象
        invocation.getTarget();
        return invocation.proceed();
    }
    
    /**
     * 生成MyBatis拦截器代理对象
     */
    @Override
    public Object plugin(Object target) {
        return Interceptor.super.plugin(target);
    }
    /**
     * 设置插件属性(直接通过Spring的方式获取属性,所以这个方法一般也用不到)
     * 项目启动的时候数据就会被加载
     */
    @Override
    public void setProperties(Properties properties) {
        Interceptor.super.setProperties(properties);
    }
}

service

 @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(IntEndScaleForecastEntity entity) {
       return iIntEndScaleForecastMapper.insert(entity);
    }

entity

在这里插入图片描述

以上几个注解

在这里插入图片描述

代码

/**
 * 功能描述:
 *
 * @author Songxianyang
 * @date 2022-08-12 21:51
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface CreateTime {
}
/**
 * 功能描述:
 *
 * @author Songxianyang
 * @date 2022-08-12 21:51
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface CreateUser {
}
/**
 * 功能描述:
 *
 * @author Songxianyang
 * @date 2022-08-12 21:51
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface UpdateTime {
}

/**
 * 功能描述:
 *
 * @author Songxianyang
 * @date 2022-08-12 21:51
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface UpdateUser {
}

效果截图:

在这里插入图片描述
帮助文档

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SteveCode.

永远年轻,永远热泪盈眶

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值