SpringBoot项目中公共字段的填充

公共字段填充

思路:

利用的是SpringBoot的Aop思想自定义注解反射机制的方法来实现

项目中我涉及公共字段的有createTime、updateTime、createUser、updateUser

步骤:

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

/**

 * 数据库操作类型 使用的是枚举方法
 */
public enum OperationType {
​
    /**
     * 更新操作
     */
    UPDATE,
​
    /**
     * 插入操作
     */
    INSERT
​
}
/**
 * 自定义注解,用于标识某个方法需要进行功能字段自动填充处理
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
​
    //数据库操作类型:UPDATE INSERT
    OperationType value();
}

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

/**
 * 自定义切面,实现公共字段字段填充处理逻辑
 */
@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){
        log.info("开始进行公共字段的填充...");
​
        //获取到当前被拦截方法上的数据库操作类型
        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 enity = args[0];
​
        //准备赋值的数据
        Long id = BaseContext.getCurrentId();
        LocalDateTime localDateTime = LocalDateTime.now();
​
        //根据当前不同的操作类型,为对应额属性通过反射来赋值
        if(type == OperationType.INSERT){
            //为四个公共字段赋值
            try {
                Method setCreateTime = enity.getClass().getMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setUpdateTime = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setCreateUser = enity.getClass().getMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateUser = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
​
                setCreateTime.invoke(enity,localDateTime);
                setUpdateTime.invoke(enity,localDateTime);
                setCreateUser.invoke(enity,id);
                setUpdateUser.invoke(enity,id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(type == OperationType.UPDATE){
            //为两个公共字段赋值
            try {
                Method setUpdateTime = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
                Method setUpdateUser = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);
​
                setUpdateTime.invoke(enity,localDateTime);
                setUpdateUser.invoke(enity,id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

3. 在Mapper的需要自动填充公共字段的方法上加入AutoFill注解

    @Insert("insert into category (type, name, sort, status, create_time, update_time, create_user, update_user) " +
            "values (#{type},#{name},#{sort},#{status},#{createTime},#{updateTime},#{createUser},#{updateUser});")
    @AutoFill(value = OperationType.INSERT)
    void save(Category category);
​
    @Delete("delete from category where id = #{id}")
    void deleteById(Long id);
​
    @AutoFill(value = OperationType.UPDATE)
    void update(Category category);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值