多表公共字段自动填充aop

通过AOP和自定义注解,实现对数据库操作时的创建时间和修改时间等公共字段的自动填充。定义枚举类OperationType表示操作类型,创建自定义注解AutoFill标识需要填充的方法,并在切面类中利用反射进行赋值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在项目开发中,遇到多个表都有字段创建时间,创建人,修改时间,修改人等,在创建实体对象的时候,这些字段需要我们手动添加,特别麻烦。那么有没有一种方式能够做到自动填充?这里我提供一种aop的方法。

步骤一:定义枚举类

在数据库进行修或者新增操作是需要插入或修改创建时间、修改时间、修改人等字段,因此定义这update和insert两个操作类型。enum表示这是一个枚举类,可以用来定义一些常量。

/**
 * 数据库操作类型
 */
public enum OperationType {

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

    /**
     * 插入操作
     */
    INSERT

}

步骤二:自定义注解类,指定注解加在注解上,用于标识哪些方法需要需要进行字段自动填充,

@interface不是接口是注解类,在jdk1.5之后加入的功能,使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节

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

步骤三:自定义切面类,aop利用反射进行公共字段的赋值

注意点:1.定义切入点时,不仅要识别到mapper包下的所有方法,而且还得是有AutoFill注解的方法;

               2.采用前置通知,在更新或者插入之前,将传过来的实体类里面的要公共字段的字段进行赋值,因为我们没有手动的去赋值,因此传过来的比如                   employee对象中的createUser等字段是空的或者是更新前的;


/**
 * 自定义切面,实现公共字段自动填充处理逻辑
 */

@Aspect
@Component
@Slf4j
public class AutoFillAspect {

    /**
     * 切入点
     */
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut(){}

    /**
     * 前置通知,在通知中进行公共字段的赋值
     */
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint){
        //可先进行调试,是否能进入该方法 提前在mapper方法添加AutoFill注解
        log.info("开始进行公共字段自动填充...");

        //获取到当前被拦截的方法上的数据库操作类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();//方法签名对象
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);//获得方法上的注解对象
        OperationType operationType = autoFill.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){
            try {
                //四个字段都要赋值
                Method setCreateTime = entity.getClass().getDeclaredMethod(SET_CREATE_TIME, LocalDateTime.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(SET_UPDATE_TIME, LocalDateTime.class);
                Method setCreateUser = entity.getClass().getDeclaredMethod(SET_CREATE_USER, Long.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(SET_UPDATE_USER, Long.class);

                //通过反射为对象属性赋值
                setCreateTime.invoke(entity,now);
                setUpdateTime.invoke(entity,now);
                setCreateUser.invoke(entity,currentId);
                setUpdateUser.invoke(entity,currentId);
            }catch (Exception e){
                e.printStackTrace();
            }
        }else {
            try {
                //两个字段都要赋值
                Method setUpdateTime = entity.getClass().getDeclaredMethod(SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(SET_UPDATE_USER, Long.class);

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

        }

    }
}

步骤四:在mapper的方法上面加上AutoFill注解

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

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值