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

公共字段自动填充

问题分析

  • 总会有些公共字段,例如创建时间和创建人

在这里插入图片描述

实现思路

  • 对mapper定义注解,使用切面思想来判断是不是更新和新增操作
  • 对于指定的操作来更新公共字段

在这里插入图片描述

自定义操作类型

在这里插入图片描述

package com.sky.enumeration;/*** 数据库操作类型*/
public enum OperationType {/*** 更新操作*/UPDATE,/*** 插入操作*/INSERT}

自定义注解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 {OperationType value();
}

自定义切面

  1. 定义切点
  2. 获取当前的mapper方法的具体操作类型
  3. 获取当前的mapper方法的参数 实体对象entity
  4. 获取要填充的数据
  5. 根据操作类型进行填充
package com.sky.aspect;import cn.hutool.core.util.ObjectUtil;
import com.sky.annotation.AutoFill;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import com.sky.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
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.time.LocalDateTime;import static com.sky.constant.AutoFillConstant.*;
import static com.sky.constant.MessageConstant.AUTO_FILL_FAILED;/*** 自动填充切面*/
@Aspect
@Component
@Slf4j
public class AutoFillAspect {// 切入点// mapper包下的所有方法 所有的参数类型@Pointcut("execution(* com.sky.mapper..*.*(..)) && @annotation(com.sky.annotation.AutoFill)")public void autoFillPointCut() {}// 前置通知 在方法执行之前执行// 进行自动填充@Before("autoFillPointCut()")public void before(JoinPoint joinPoint) {log.info("自动填充切面执行");// 获取当前的mapper方法的具体操作类型MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();OperationType operationType = methodSignature.getMethod().getAnnotation(AutoFill.class).value();// 获取当前的mapper方法的参数 实体对象entityObject[] args = joinPoint.getArgs();//非空判断if(ObjectUtil.isEmpty(args)) return;// 获取实体对象Object entity = args[0];// 获取要填充的数据// 获取当前登录用户的信息Long currentUserId = BaseContext.getCurrentId();// 获取当前时间LocalDateTime now = LocalDateTime.now();// 根据操作类型进行填充switch (operationType) {case INSERT:// 插入操作try {entity.getClass().getMethod(SET_CREATE_USER, Long.class).invoke(entity, currentUserId);entity.getClass().getMethod(SET_UPDATE_USER, Long.class).invoke(entity, currentUserId);entity.getClass().getMethod(SET_CREATE_TIME, LocalDateTime.class).invoke(entity, now);entity.getClass().getMethod(SET_UPDATE_TIME, LocalDateTime.class).invoke(entity, now);} catch (Exception e) {log.error(AUTO_FILL_FAILED, e);throw new BaseException(AUTO_FILL_FAILED);}break;case UPDATE:// 更新操作try {entity.getClass().getMethod(SET_UPDATE_USER, Long.class).invoke(entity, currentUserId);entity.getClass().getMethod(SET_UPDATE_TIME, LocalDateTime.class).invoke(entity, now);} catch (Exception e) {log.error(AUTO_FILL_FAILED, e);throw new BaseException(AUTO_FILL_FAILED);}break;default:break;}}
}

使用

    @AutoFill(OperationType.UPDATE)void update(Category category);
    @AutoFill(OperationType.INSERT)@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})")void insert(Category category);
http://www.lryc.cn/news/445861.html

相关文章:

  • 超详细 Git 教程:二十篇博客,三万字干货
  • “出参”和“入参”的命名由来
  • webrtc gclient sync报错问题解决
  • FLUX模型,或许这几点你还未曾都了解,最详细的Flux模型介绍(附模型安装包)
  • RAG(Retrieval-Augmented Generation)检索增强生成技术基础了解学习与实践
  • 基于SpringBoot实现高性能缓存组件
  • 【深度学习基础模型】递归神经网络 (Recurrent Neural Networks, RNN) 详细理解并附实现代码。
  • python全栈学习记录(十九) hashlib、shutil和tarfile、configparser
  • RL进阶(一):变分推断、生成模型、SAC
  • WPF 绑定 DataGrid 里面 Button点击事件 TextBlock 双击事件
  • 828华为云征文|华为云Flexus云服务器X实例Windows系统部署一键短视频生成AI工具moneyprinter
  • 非标精密五金加工的技术要求
  • 新手小白怎么通过云服务器跑pytorch?
  • Spring 全家桶使用教程
  • Spark SQL性能优化高频面试题及答案
  • 云原生链路观测平台 openobserve + fluent-bit,日志收集
  • Android 车载应用开发指南 - CarService 详解(下)
  • 【Linux网络 —— 网络基础概念】
  • el-form动态标题和输入值,并且最后一个输入框不校验
  • 一,初始 MyBatis-Plus
  • 安卓13删除下拉栏中的关机按钮版本2 android13删除下拉栏关机按钮
  • 快递物流单号识别API接口代码
  • AI时代的程序员:如何保持和提升核心竞争力
  • Oracle 数据库常用命令与操作指南
  • spring boot项目对接人大金仓
  • 《操作系统 - 清华大学》1 -2:操作系统概述 —— 什么是操作系统
  • power bi制作各季度收入累加柱状图——日期表、calculate、datesytd
  • OceanBase 3.X 高可用 (一)
  • CSR、SSR、SSG
  • linux -L16-linux 查看应用占用的资源top