網站首頁 編程語言 正文
以create_time、update_time、create_user、update_user字段為例
實現步驟:
第一步:創建一個枚舉
public enum OperationType {
? ? /**
? ? ?* 更新操作
? ? ?*/
? ? UPDATE,
? ? /**
? ? ?* 插入操作
? ? ?*/
? ? INSERT
}
第二步:自定義一個注解
/**
?* 自定義注解,用于標識某個方法需要進行功能字段自動填充處理
?*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
? ? //數據庫操作類型:UPDATE INSERT
? ? OperationType value();
}
第三步:在需要填充的Mapper層的方法上加上相應的注解,eg:
第四步:通過aop使得添加的注解生效
@Aspect
@Component
public class AutofillAspect {
? ? @Before("execution(* com.sky.mapper.*.*(..)) && @annotation(autoFill)")
? ? public void autoFill(JoinPoint joinPoint, AutoFill autoFill){
? ? ? ? log.info("開始自動填充公共字段值");
? ? ? ? //獲取Mapper方法的參數:entity對象
? ? ? ? Object[] args = joinPoint.getArgs();
? ? ? ? if (args == null || args.length == 0) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? Object entity = args[0];
? ? ? ? //準備要賦的值
? ? ? ? LocalDateTime now = LocalDateTime.now();
? ? ? ? Long currentUser = BaseContext.getCurrentId();
? ? ? ? //判斷操作的類型
? ? ? ? Class<?> clazz = entity.getClass();
? ? ? ? if (autoFill.value() == OperationType.INSERT) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? //是新增操作:獲取setCreateTime、setUpdateTime、setCreateUser、setUpdateUser四個方法
? ? ? ? ? ? ? ? Method setCreateTimeMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
? ? ? ? ? ? ? ? Method setUpdateTimeMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
? ? ? ? ? ? ? ? Method setCreatorMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
? ? ? ? ? ? ? ? Method setUpdatorMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
? ? ? ? ? ? ? ? //使用反射調用entity的對應方法,給屬性賦值
? ? ? ? ? ? ? ? setCreateTimeMethod.invoke(entity, now);
? ? ? ? ? ? ? ? setUpdateTimeMethod.invoke(entity, now);
? ? ? ? ? ? ? ? setCreatorMethod.invoke(entity, currentUser);
? ? ? ? ? ? ? ? setUpdatorMethod.invoke(entity, currentUser);
? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }else{
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? //是修改操作:獲取setUpdateTime、setUpdateUser兩個方法
? ? ? ? ? ? ? ? Method setUpdateTimeMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
? ? ? ? ? ? ? ? Method setUpdatorMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
? ? ? ? ? ? ? ? //使用反射調用entity的對應方法,給屬性賦值
? ? ? ? ? ? ? ? setUpdateTimeMethod.invoke(entity, now);
? ? ? ? ? ? ? ? setUpdatorMethod.invoke(entity, currentUser);
? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
最后就能夠正常使用了
原文鏈接:https://blog.csdn.net/Hacker_Hacker007/article/details/136127410
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-04-17 Failed to bind properties under spring.servlet.mul
- 2022-06-14 go語言中的udp協議及TCP通訊實現示例_Golang
- 2022-03-29 python中apply函數詳情_python
- 2022-07-11 UVM中analysis端口的使用方法
- 2022-05-12 小程序自定義日期組件,不顯示今日之后的日期
- 2022-06-16 Golang協程池gopool設計與實現_Golang
- 2022-06-24 python函數常見關鍵字分享_python
- 2022-12-15 C++?Boost?Lockfree超詳細講解使用方法_C 語言
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支