关于公共字段自动填充的代码实现

本文介绍了如何在Java中使用自定义注解AutoFill和切面编程实现Mapper方法的公共字段(如创建时间、更新时间、用户信息)自动填充,区分插入和更新操作的过程。
摘要由CSDN通过智能技术生成

概要

在常用的编辑信息时需要自动填充公共字段,比如更新时间字段和修改人字段等等

整体架构流程

自定义注解AutoFill,用于表示需要进行公共字段自动填充的方法

自定义切面类AutoFillAspect,同一拦截加入了AutoFill注解的方法

在Mapper方法上加上AutoFill

具体步骤

1.创建一个annotation包,在包下创建一个AutoFill注解类

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
    
    OperationType value();
}

OperationType是一个枚举类  

代码如下

public enum OperationType {

    /**
     * 更新操作
     */
    UPDATE,

    /**
     * 插入操作
     */
    INSERT

}

2.创建一个包aspect ,在包下创建AutoFillAspect类

这是一个切面类,作用是拦截mapper里面所有加了@AutoFill注解的方法

@Aspect  //切面类
@Component
@Slf4j
public class AutoFillAspect {

    /*切入点
     * */

    //锁定了mapper类下面所有的方法  同时还要满足方法上加入了@AutoFill注解
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut() {  //annotation注解
    }

    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinpoint) {
        log.info("开始进行公共字段填充");
    }
}

3.然后在mapper层类的方法上加上AutoFill注解

@AutoFill(value = OperationType.INSERT)

这是一个mapper层下的新增员工接口和更新员工接口

@AutoFill(value = OperationType.INSERT)
    int insert(Employee employee);


@AutoFill(value = OperationType.UPDATE)
     void update(Employee employee);

4.现在开始编写 切面类AutoFillAspect , autoFill方法里

的代码

要判断加上@AutoFill注解的方法是更新操作还是新增操作

如果是新增操作,则需要

更新这4个字段setCreateTime setCreateUser setUpdateTime setUpdateUser

如果是更新操作,则需要

更新这2个字段 setUpdateTime setUpdateUser

具体代码如下:

 public void autoFill(JoinPoint joinpoint) {
        log.info("开始进行公共字段填充");

        //获取当前被拦截的方法上的数据库操作类型
        MethodSignature signature = (MethodSignature) joinpoint.getSignature(); //方法签名对象
        AutoFill annotation = signature.getMethod().getAnnotation(AutoFill.class);//获得方法上的注解对象
        OperationType operationType = annotation.value();//获得数据库操作类型

        //获取到当前被拦截的方法的参数
        Object[] args = joinpoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }
        Object entity = args[0];

        //准备需要赋值的数据
        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();

        //根据不同的操作类型,为对应的属性通过反射赋值
        if (operationType==OperationType.INSERT){
            //4个字段
            try {
                Method setCreateTime = entity.getClass().getDeclaredMethod("setCreateTime", LocalDateTime.class);
                Method setCreateUser = entity.getClass().getDeclaredMethod("setCreateUser", Long.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod("setUpdateUser", Long.class);

                //通过反射为对象赋值
                setCreateTime.invoke(entity,now);
                setCreateUser.invoke(entity,currentId);
                setUpdateTime.invoke(entity,now);
                setUpdateUser.invoke(entity,currentId);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        else if (operationType==OperationType.UPDATE){
            //2个字段
            try {
                Method  setUpdateTime = entity.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod("setUpdateUser", Long.class);
                //通过反射为对象赋值
                setUpdateTime.invoke(entity,now);
                setUpdateUser.invoke(entity,currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }



        }
    }

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值