JAVA项目开发公共字段自动填充——省事

  业务表中,有时候会有公共的字段需要反复的赋值,最常见的就是每次操作都会记录更新日期,更新人等。

     为了避免反复的赋值操作—— 我们可以用自定义注解加上自定义切面,还有反射来一次性设置多次使用。

 1.首先自定义一个注解,用于表示需要进行公共字段填充的方法:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
//    数据库操作类型:UPDATE INSERT
    OperationType value();
}

这段代码是一个自定义注解 AutoFill,用于标记方法,并指定一个注解参数 value() 来表示数据库的操作类型。下面是对这段代码的解释:

  • @Target(ElementType.METHOD):表示该注解可以应用于方法上。
  • @Retention(RetentionPolicy.RUNTIME):表示该注解在运行时仍然可用,可以通过反射来获取注解信息。
  • public @interface AutoFill:定义了一个名为 AutoFill 的注解。

注解参数部分的解释如下:

  • OperationType value():该注解参数表示数据库的操作类型,类型为 OperationType 。OperationType 可以是一个枚举类型,用来表示数据库的更新操作(如更新数据、插入数据等)。
  • 枚举示例(表示数据库操作的类型)

通过使用这个自定义注解,开发者可以在方法上标记需要进行自动填充的操作,并通过 value() 参数指定数据库的操作类型,用于后续的处理和判断。

2.自定义切面类AutoFillAspect,统一拦截加入了AutoFill注解的方法,通过反射为公共字段赋值。

@Aspect
@Component
@Slf4j
public class AutoFillAspect {
    /**
     * 切入点 (对哪些类的哪些方法进行拦截)
     * @PointCut(切点表达式)
     *  表示匹配 com.sky.mapper 包及其子包中的所有类的所有方法,并且这些方法必须同时带有 com.sky.annotation.AutoFill 注解。
     */
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut(){}


    /**
     * 自定义前置通知:在通知中进行公共字段的赋值
     */
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint)  {
        log.info("开始进行公共字段自动填充..");

        //1.获取到当前被拦截的方法上的数据库操作类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();//方法签名对象
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);//获得方法上的注解对象
        OperationType op = autoFill.value();//获取数据库操作类型
        //2.获取到当前被拦截的方法的参数--实体对象
        Object[] args = joinPoint.getArgs();
        if(args == null || args.length ==0) {
            return;
        }
        //约定好实体类放在参数的第一个位置
        Object entity = args[0];
        //3.准备赋值数据
        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();

        //4.根据当前不同的操作类型,为对应的属性通过反射赋值
        if(op == OperationType.INSERT) {//如果是插入操作
            //为4个都赋值

            try {
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, 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(op == OperationType.UPDATE) {
               try {
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

                //通过反射为对象属性赋值
                setUpdateTime.invoke(entity,now);
                setUpdateUser.invoke(entity,currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }
}

3.在Mapper的方法上加入AutoFill注解

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值