欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot項目中公共字段填充的實現(xiàn)

 更新時間:2023年10月08日 16:10:32   作者:終有救贖  
本文主要介紹了SpringBoot項目中公共字段填充的實現(xiàn),利用SpringBoot的Aop思想和自定義注解和反射機制的方法來實現(xiàn),具有一定的參考價值,感興趣的可以了解一下

思路:

利用的是SpringBoot的Aop思想自定義注解反射機制的方法來實現(xiàn)

項目中我涉及公共字段的有createTime、updateTime、createUser、updateUser

步驟:

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

/**
 * 數(shù)據(jù)庫操作類型 使用的是枚舉方法
 */
public enum OperationType {

    /**
     * 更新操作
     */
    UPDATE,

    /**
     * 插入操作
     */
    INSERT
}
/**
 * 自定義注解,用于標識某個方法需要進行功能字段自動填充處理
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {

    //數(shù)據(jù)庫操作類型:UPDATE INSERT
    OperationType value();
}

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

/**
 * 自定義切面,實現(xiàn)公共字段字段填充處理邏輯
 */
@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("開始進行公共字段的填充...");

        //獲取到當前被攔截方法上的數(shù)據(jù)庫操作類型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        OperationType type = autoFill.value();

        //獲取到當前被攔截方法的參數(shù)--實體對象
        Object[] args = joinPoint.getArgs();
        if(args == null || args.length!=0){
            return;
        }
        Object enity = args[0];

        //準備賦值的數(shù)據(jù)
        Long id = BaseContext.getCurrentId();
        LocalDateTime localDateTime = LocalDateTime.now();

        //根據(jù)當前不同的操作類型,為對應(yīng)額屬性通過反射來賦值
        if(type == OperationType.INSERT){
            //為四個公共字段賦值
            try {
                Method setCreateTime = enity.getClass().getMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setUpdateTime = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setCreateUser = enity.getClass().getMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateUser = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

                setCreateTime.invoke(enity,localDateTime);
                setUpdateTime.invoke(enity,localDateTime);
                setCreateUser.invoke(enity,id);
                setUpdateUser.invoke(enity,id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(type == OperationType.UPDATE){
            //為兩個公共字段賦值
            try {
                Method setUpdateTime = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
                Method setUpdateUser = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);

                setUpdateTime.invoke(enity,localDateTime);
                setUpdateUser.invoke(enity,id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

3. 在Mapper的需要自動填充公共字段的方法上加入AutoFill注解

    @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});")
    @AutoFill(value = OperationType.INSERT)
    void save(Category category);

    @Delete("delete from category where id = #{id}")
    void deleteById(Long id);

    @AutoFill(value = OperationType.UPDATE)
    void update(Category category);

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

  • Freemarker如何生成樹形導航菜單(遞歸)

    Freemarker如何生成樹形導航菜單(遞歸)

    這篇文章主要為大家詳細介紹了Freemarker采用的的方法生成樹形導航菜單,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • 淺談HashMap中7種遍歷方式的性能分析

    淺談HashMap中7種遍歷方式的性能分析

    本文先從HashMap的遍歷方法講起,然后再從性能、原理以及安全性等方面,來分析HashMap各種遍歷方式的優(yōu)勢與不足
    2021-06-06
  • Java 實戰(zhàn)項目之家居購物商城系統(tǒng)詳解流程

    Java 實戰(zhàn)項目之家居購物商城系統(tǒng)詳解流程

    讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實現(xiàn)一個家居購物商城系統(tǒng),大家可以在過程中查缺補漏,提升水平
    2021-11-11
  • SpringBoot返回Json對象報錯(返回對象為空{(diào)})

    SpringBoot返回Json對象報錯(返回對象為空{(diào)})

    本文主要介紹介紹了SpringBoot返回Json對象報錯(返回對象為空{(diào)}),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • java開發(fā)環(huán)境的完整搭建過程

    java開發(fā)環(huán)境的完整搭建過程

    這篇文章主要給大家介紹了關(guān)于java開發(fā)環(huán)境的完整搭建過程,文中介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02
  • App登陸java后臺處理和用戶權(quán)限驗證

    App登陸java后臺處理和用戶權(quán)限驗證

    這篇文章主要為大家詳細介紹了App登陸java后臺處理和用戶權(quán)限驗證,感興趣的朋友可以參考一下
    2016-06-06
  • Java?HashTable與Collections.synchronizedMap源碼深入解析

    Java?HashTable與Collections.synchronizedMap源碼深入解析

    HashTable是jdk?1.0中引入的產(chǎn)物,基本上現(xiàn)在很少使用了,但是會在面試中經(jīng)常被問到。本文就來帶大家一起深入了解一下Hashtable,需要的可以參考一下
    2022-11-11
  • 一文快速了解spring?boot中的@idempotent注解

    一文快速了解spring?boot中的@idempotent注解

    idempotence注解是RESTful API設(shè)計中一個重要的概念,它可以保證操作的可靠性和一致性,下面這篇文章主要給大家介紹了關(guān)于spring?boot中@idempotent注解的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • Java SimpleDateFormat線程安全問題原理詳解

    Java SimpleDateFormat線程安全問題原理詳解

    這篇文章主要介紹了Java SimpleDateFormat線程安全問題原理詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • 最新評論