小工具知识网

当前位置: 首页 > 行业知识 > 公共字段自动填充

行业知识

公共字段自动填充

2026-11-02 14:39:57 来源:互联网转载

在开发��经常面临对于一些公共字段的赋值。

如在下表中:

如何让程序自动为我们需要赋值的公共字段进行赋值,避免在业务代码中重复写这些公共字段的赋值代码

如下图所示:

实现思路:

1.自定义注解AutoFill,用于标识需要进行公共字段自动填充的方法

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

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

实现步骤:

1.自定义注解

package com.yang.annotation;import com.yang.enumeration.OperationType;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 自定义注解,用于标识某个方法需要公共字段填充处理 */@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface AutoFill {    //指定数据库操作类型: UPDATE INSERT    OperationType value();}

2.自定义切面类AutoFillAspect

package com.yang.aspect;import com.yang.annotation.AutoFill;import com.yang.constant.AutoFillConstant;import com.yang.context.BaseContext;import com.yang.enumeration.OperationType;import lombok.extern.slf4j.Slf4j;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.Signature;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.stereotype.Component;import java.lang.reflect.Method;import java.time.LocalDateTime;/** * 自定义切面,实现公共字段填充处理逻辑 */@Aspect@Component@Slf4jpublic class AutoFillAspect {    //指定切入点    @Pointcut("execution(* com.yang.mapper.*.*(..)) && @annotation(com.yang.annotation.AutoFill)")    public void autoFillPointCut(){    }    /**     * 设置前置通知为公共字段赋值     */    @Before("autoFillPointCut()")    public void autoFill(JoinPoint joinPoint){        log.info("开始公共字段的填充...");        //获取当前被拦截的方法的数据库类型        //方法签名对象        //因为需要访问特定于方法的信息,比如方法名、返回类型、参数类型等,所以需要向下转型为 MethodSignature        MethodSignature signature = (MethodSignature)joinPoint.getSignature();        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);        OperationType type = autoFill.value();        //获取被拦截方法的参数--实体对象        Object[] args = joinPoint.getArgs();        if (args==null||args.length ==0){            return;        }        Object entity = args[0];        //准备赋值的数据        LocalDateTime time = LocalDateTime.now();        Long id = BaseContext.getCurrentId();        //根据当前不同的操作类型,为对应的属性赋值(通过反射)        if (type.equals(OperationType.INSERT)){            //为四个公共字段赋值            try {                //使用反射动态获取set方法并且指定方法所接收的参数类型                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);                Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);                //通过反射进行赋值                setCreateTime.invoke(entity,time);                setUpdateTime.invoke(entity,time);                setCreateUser.invoke(entity,id);                setUpdateUser.invoke(entity,id);            } catch (Exception e) {                throw new RuntimeException(e);            }        }else if (type.equals(OperationType.UPDATE)){            //为两个公共字段赋值            try {                //使用反射动态获取set方法并且指定方法所接收的参数类型                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,time);                setUpdateUser.invoke(entity,id);            } catch (Exception e) {                throw new RuntimeException(e);            }        }    }}

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

比如:

4.测试

修改前的Category表

修改方法:

新增方法:

表结果:

参考:Day03-03-公共字段自动填充_代码开发_2_哔哩哔哩_bilibili

上一篇:轻松掌握!Studio One下载安装的详细步骤教程

下一篇:怎么还原ubuntu服务器?