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

SpringBoot 项目公共字段填充

[公共字段自动填充]

核心:在切面类中捕获需要填充公共字段的 Mapper 方法,方法上使用注解加以标识,通过反射拿到需要填充的字段赋值方法,进行赋值操作

1、自定义注解 AutoFill

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {//数据库操作类型:UPDATE INSERTOperationType value();
}

2、枚举对象类

public enum OperationType {/*** 更新操作*/UPDATE,/*** 插入操作*/INSERT
}

3、自定义切面 AutoFillAspect

@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 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();//就是获取修改人的id值,不同项目不同的获取方法//根据当前不同的操作类型,为对应的属性通过反射来赋值if(operationType == OperationType.INSERT){//为4个公共字段赋值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) {e.printStackTrace();}}else if(operationType == OperationType.UPDATE){//为2个公共字段赋值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) {e.printStackTrace();}}}
}

4、在Mapper接口的方法上加入 AutoFill 注解

@Mapper
public interface CategoryMapper {/*** 插入数据* @param category*/@AutoFill(value = OperationType.INSERT)void insert(Category category);/*** 根据id修改分类* @param category*/@AutoFill(value = OperationType.UPDATE)void update(Category category);}
http://www.lryc.cn/news/226559.html

相关文章:

  • 分布式搜索引擎ES
  • 2023年05月 Python(四级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • @KafkaListener注解详解(一)| 常用参数详解
  • 蓝桥杯算法双周赛心得——被替换的身份证(分类讨论)
  • 实验一:人工智能之启发式搜索算法(含源码+实验报告)
  • C++学习 --类和对象之封装
  • 在Spring Boot中使用JTA实现对多数据源的事务管理
  • 介绍YOLO-NAS Pose:姿势估计的技术
  • 计算机毕业设计 基于SpringBoot的实训管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • Python开发运维:Python3.7使用QQ邮箱发送不同类型邮件
  • 二十三种设计模式全面解析-解密迭代器模式:探索遍历之道
  • kubernetes istio
  • 25期代码随想录算法训练营第十四天 | 二叉树 | 递归遍历、迭代遍历
  • 常用布局以及其优缺点
  • 海康工业相机如何提高相机帧率
  • Linux之IPC通信共享内存(一次拷贝)与消息队列、管道、信号量、socket(两次拷贝)总结(六十二)
  • 【多线程 - 01、概述】
  • SQL SELECT INTO 语句
  • 【刷题】(AtCoder Beginner Contest 328) C、D 补题
  • NI USRP软件无线设备的特点
  • 大数据毕业设计选题推荐-污水处理大数据平台-Hadoop-Spark-Hive
  • 最新获取支付宝cardIndex参数图文教程
  • Linux学习第二枪(yum,vim,g++/gcc,makefile的使用)
  • 自然语言处理(一):RNN
  • 超全总结!大模型算法面试指南(含答案)
  • 前端使用C-lodop 实现循环套打小案例
  • 基于SpringBoot+Vue+mysql卓越导师双选系统设计与实现
  • Windows 11系统cmd终端美化、Vscode终端美化
  • [游戏中的图形学实时渲染技术] Part1 实时阴影技术
  • NtripShare Mos地铁自动化监测终端盒子硬件设计