0
点赞
收藏
分享

微信扫一扫

深度学习基础入门:从数学到实现

JakietYu 22小时前 阅读 0

一、公共字段填充功能

1. 自定义切面 AutoFillAspect 的 autoFill 方法

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

这段代码是使用 Spring AOP(面向切面编程)来获取方法签名及方法上的注解信息。

  • joinPoint.getSignature():这是 Spring AOP 中的一个方法,用于获取连接点(Join Point)的签名信息,即被代理方法的相关信息,比如方法名、参数等。返回的类型是 Signature 类型的对象。
  • getMethod().getAnnotation(AutoFill.class):这一步是获取方法上的注解对象。代码试图获取被代理方法上是否存在 AutoFill 注解。如果存在,则返回该注解的实例;如果不存在,则返回 null。

  • autoFill.value():这一步是获取注解实例中的 value 字段值,即获取数据库操作类型。

总之,可以先通过joinPoint.getSignature()获得连接点处的方法签名对象,再通过signature.getMethod().getAnnotation(AutoFill.class)获得代理方法上的AutoFill注解的实例,最后通过.value()方法获取注解的value,也就是数据库的操作类型

// 根绝当前不同的操作类型,为属性进通过反射来进行赋值
        if(operationType==OperationType.INSERT){
            try {
                // 插入字段,需要为4个公共字段赋值
                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();
            }
        }

首先,根据给定的数据库操作类型(operationType)如插入、更新等。

在插入操作中,通常会对一些公共字段进行赋值,比如创建时间、创建用户、更新时间、更新用户等。通过反射invoke)获取实体类(entity)中对应的方法,这些方法通常用于设置这些公共字段的值。比如,setCreateTime 方法用于设置创建时间,setCreateUser 方法用于设置创建用户,以此类推。

接下来,通过反射调用这些方法,并传入相应的参数,即当前的时间(now)和当前用户的ID(currentId),来为实体类的属性赋值。

举报

相关推荐

0 条评论