亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用SpringAOP實現(xiàn)公共字段填充功能

 更新時間:2024年08月06日 09:13:20   作者:未來的JAVA高級開發(fā)工程師  
在新增員工或者新增菜品分類時需要設(shè)置創(chuàng)建時間、創(chuàng)建人、修改時間、修改人等字段,在編輯員工或者編輯菜品分類時需要設(shè)置修改時間、修改人等字段,這些字段屬于公共字段,本文將給大家介紹使用SpringAOP實現(xiàn)公共字段填充功能,需要的朋友可以參考下

概要

新增員工或者新增菜品分類時需要設(shè)置創(chuàng)建時間、創(chuàng)建人、修改時間、修改人等字段,在編輯員工或者編輯菜品分類時需要設(shè)置修改時間、修改人等字段。這些字段屬于公共字段,也就是也就是在我們的系統(tǒng)中很多表中都會有這些字段,如下:

序號字段名含義數(shù)據(jù)類型
1create_time創(chuàng)建時間datetime
2create_user創(chuàng)建人idbigint
3update_time修改時間datetime
4update_user修改人idbigint

而針對于這些字段,我們的賦值方式為:

1). 在新增數(shù)據(jù)時, 將createTime、updateTime 設(shè)置為當前時間, createUser、updateUser設(shè)置為當前登錄用戶ID。

2). 在更新數(shù)據(jù)時, 將updateTime 設(shè)置為當前時間, updateUser設(shè)置為當前登錄用戶ID。

我們使用AOP切面編程,實現(xiàn)功能增強,來完成公共字段自動填充功能。

技術(shù)細節(jié)

在實現(xiàn)公共字段自動填充,也就是在插入或者更新的時候為指定字段賦予指定的值,使用它的好處就是可以統(tǒng)一對這些字段進行處理,避免了重復代碼。在上述的問題分析中,我們提到有四個公共字段,需要在新增/更新中進行賦值操作, 具體情況如下:

序號字段名含義數(shù)據(jù)類型操作類型
1create_time創(chuàng)建時間datetimeinsert
2create_user創(chuàng)建人idbigintinsert
3update_time修改時間datetimeinsert、update
4update_user修改人idbigintinsert、update

實現(xiàn)步驟:

1). 自定義注解 AutoFill,用于標識需要進行公共字段自動填充的方法

2). 自定義切面類 AutoFillAspect,統(tǒng)一攔截加入了 AutoFill 注解的方法,通過反射為公共字段賦值

3). 在 Mapper 的方法上加入 AutoFill 注解

若要實現(xiàn)上述步驟,需掌握以下知識

技術(shù)點:枚舉、注解、AOP、反射

  • 枚舉類:
package com.sky.enumeration;
 
/**
 * 數(shù)據(jù)庫操作類型
 */
public enum OperationType {
 
    /**
     * 更新操作
     */
    UPDATE,
 
    /**
     * 插入操作
     */
    INSERT
 
}
  • 自定義注解:
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 {
    //數(shù)據(jù)庫操作類型:UPDATE INSERT
    OperationType value();
}
  •  自定義切面類:
  1. 公共字段填充,我們選擇在連接點(方法)執(zhí)行前就進行,所以用@Before
  2. 將連接點上的@AutoFill注解攔截下
  3. 利用注解的.value()獲取到注解中的內(nèi)容(數(shù)據(jù)庫操作類型)
  4. 獲取到該連接點的參數(shù)
  5. 準備好填充的內(nèi)容
  6. 利用反射根據(jù)數(shù)據(jù)庫操作類型進行填充
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 org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
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.annotation.Annotation;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
 
@Aspect
@Slf4j
@Component
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("進行公共字段賦值");
        //獲取當前被攔截方法上的注解
        AutoFIll autoFIll = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(AutoFIll.class);
        //MethodSignature signature = (MethodSignature) joinPoint.getSignature();//方法簽名對象
        //AutoFIll autoFill = signature.getMethod().getAnnotation(AutoFIll.class);
        //獲取數(shù)據(jù)庫操作類型(INSERT,UPDATE)
        OperationType operationType = autoFIll.value();
        //獲取當前被攔截方法上的參數(shù)(實體對象)
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }
        Object entity = args[0];
        //準備要填充的內(nèi)容
        LocalDateTime now = LocalDateTime.now();
        Long id = BaseContext.getCurrentId();
        //根據(jù)數(shù)據(jù)庫操作類型進行賦值
        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, id);
                setUpdateTime.invoke(entity, now);
                setUpdateUser.invoke(entity, id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } 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, id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
@AutoFIll(value = OperationType.INSERT)
    void insert(Dish dish);

效果展示

到此這篇關(guān)于使用SpringAOP實現(xiàn)公共字段填充功能的文章就介紹到這了,更多相關(guān)SpringAOP公共字段填充內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論