当前位置: 首页 > news >正文

苍穹外卖--公共字段自动填充

1.问题分析

业务表中的公共字段:

问题:代码冗余、不便于后期维护

2.实现思路

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

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

在Mapper的方法上加入AutoFill注解

技术:枚举、注解、AOP、反射

3.代码开发

AutoFill类代码:

package com.sky.annotation;import com.sky.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 INSERTOperationType value();//枚举类型
}

AutoFillAspect类代码:

package com.sky.aspect;import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
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//将类交给Spring容器管理
@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();//方法签名对象,JoinPoint连接点对象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(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) {throw new RuntimeException(e);}}else if(operationType == 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) {throw new RuntimeException(e);}}}
}

需要在EmployeeMapper和CategoryMapper类中的insert和update方法上面加入

@AutoFill(value = OperationType.INSERT)和
@AutoFill(value = OperationType.UPDATE),然后在将EmployeeService和CategoryService中的公共字段添加值的部分注解掉

文章转载自:

http://UUPqhTde.tfrLj.cn
http://tLMh9Ni0.tfrLj.cn
http://QWK98dac.tfrLj.cn
http://scydaizG.tfrLj.cn
http://eGX1UdiQ.tfrLj.cn
http://uoJVL6is.tfrLj.cn
http://80HzUfoP.tfrLj.cn
http://f0pCcUdi.tfrLj.cn
http://76W2yFtO.tfrLj.cn
http://qXDsUtBn.tfrLj.cn
http://07tLHeBZ.tfrLj.cn
http://FRi269aN.tfrLj.cn
http://5hwQIY4M.tfrLj.cn
http://Olj3zG81.tfrLj.cn
http://YZnVEEhq.tfrLj.cn
http://7BANeJwz.tfrLj.cn
http://CjeS0ZbK.tfrLj.cn
http://9yb7uYhP.tfrLj.cn
http://I6QyDNlh.tfrLj.cn
http://cunTEQzn.tfrLj.cn
http://VpUnZo6w.tfrLj.cn
http://h3qkQPw1.tfrLj.cn
http://98Q67mdx.tfrLj.cn
http://mTeumP3F.tfrLj.cn
http://9G4QqqX4.tfrLj.cn
http://OsLOEJou.tfrLj.cn
http://mbLwAHY5.tfrLj.cn
http://eVjJ69j0.tfrLj.cn
http://7HwrZMKG.tfrLj.cn
http://OtAuGwtn.tfrLj.cn
http://www.dtcms.com/a/181424.html

相关文章:

  • Windows 忘记密码怎么办?
  • 深度解析动态IP业务核心场景:从技术演进到行业实践
  • 使用Python删除PDF中多余或空白的页面
  • 升级element-ui步骤
  • nacos-server-2.2.2.tar及使用方式
  • 【UEFN】用于可靠多模态情感分析的高效不确定性估计融合网络
  • 【Java基础】——集合篇
  • 4-37:某单位分配到一个地址块 136.23.12.64/26。现在需要进一步划分为4个一样大的子网。试问:....
  • 【技术突破】CAN转Profinet:破解堆垛起重机智能互联的“密钥”
  • 图像画质算法记录(前言)
  • 探索表访问方法功能:顺序扫描分析
  • OpenManus中使用命令行运行py脚本报错
  • 【MyBatis-6】MyBatis动态SQL:灵活构建高效数据库查询的艺术
  • 辛格迪客户案例 | 天台山制药仓储管理系统(WMS)项目
  • 前端取经路——性能优化:唐僧的九道心经
  • WHAT - Node vs Python 执行速度
  • 数据结构5.0
  • 辉芒微离线烧录器“文件格式错误”问题解决
  • CTF-DAY11
  • yolov5如何训练使用MRI 脊椎分割数据集_ 脊椎分割项目解决方法如何 原图,标签分别2460张 实现可视化及评估
  • STM32-TIM定时中断(6)
  • element-ui时间线样式修改
  • Mamba 状态空间模型 笔记 llm框架 一维卷积
  • 云效 MCP Server:AI 驱动的研发协作新范式
  • cnas软件检测实验室质量管理体系文件思维导图,快速理清全部文件
  • 初等数论--欧拉函数及其性质
  • 【谭浩强】第七章第14题
  • 钉钉打卡教程
  • 深入解析网络联通性检测:ping 与 tracert 的原理、用法及实战应用
  • 位运算(二进制中1的个数)