自定义切面 完成公共字段填充

步骤一

自定义注解 AutoFill


/**
​
 * 自定义注解,用于标识某个方法需要进行功能字段自动填充处理
   */
​
   @Target(ElementType.METHOD)
   @Retention(RetentionPolicy.RUNTIME)
   public @interface AutoFill {
   //数据库操作类型:UPDATE INSERT
   OperationType value();
   }

OperationType


​
/**
 * 数据库操作类型
 */
public enum OperationType {
​
    /**
     * 更新操作
     */
    UPDATE,
​
    /**
     * 插入操作
     */
    INSERT
}

步骤二

自定义切面 AutoFillAspect


​
/**
 * 自定义切面,实现公共字段自动填充处理逻辑
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
​
    /**
     * 切入点
     */
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void pt() {
    }
​
    /**
     * 前置通知,在通知中进行公共字段的赋值
     */
    @Before("pt()")
    public void autoFill(JoinPoint joinPoint) {
        //1.获取签名对象
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //2.获取方法上的注解
        AutoFill autoFill = methodSignature.getMethod().getAnnotation(AutoFill.class);
        OperationType type = autoFill.value();
        //3.解析:判断是增加还是更新操作
        Object[] args = joinPoint.getArgs();
        if (Objects.isNull(args) || args.length == 0) {
            //没有参数,则不处理
            return;
        }
        try {
            //获取到当前被拦截的方法的参数--实体对象
            Object target = args[0];
            //准备赋值的数据
            LocalDateTime now = LocalDateTime.now();
            Long id = BaseContext.getCurrentId();
            Class<?> clazz = target.getClass();
            //根据当前不同的操作类型,为对应的属性通过反射来赋值
            if (type == OperationType.INSERT) {
                //为公共字段赋值
                Method setCreateTimeMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME,LocalDateTime.class);
                Method setCreateUserMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_CREATE_USER,Long.class);
                autoFillUpdateMethod(target, now, id, clazz);
                //通过反射为对象属性赋值
                setCreateTimeMethod.invoke(target, now);
                setCreateUserMethod.invoke(target, id);
            } else if (type == OperationType.UPDATE) {
                autoFillUpdateMethod(target, now, id, clazz);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
​
    /**
     * 更新操作自动填充
     * @param target
     * @param now
     * @param id
     * @param clazz
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    private void autoFillUpdateMethod(Object target, LocalDateTime now, Long id, Class<?> clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        //为公共字段赋值
        Method setUpdateTimeMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
        Method setUpdateUserMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);
        //通过反射为对象属性赋值
        setUpdateUserMethod.invoke(target, id);
        setUpdateTimeMethod.invoke(target, now);
    }
}

步骤三

在Mapper接口的方法上加入 AutoFill 注解

@AutoFill(value = OperationType.INSERT)
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值