SpringBoot自定義注解如何解決公共字段填充問題
1.1 問題分析
在新增員工或者新增菜品分類時(shí)需要設(shè)置創(chuàng)建時(shí)間、創(chuàng)建人、修改時(shí)間、修改人等字段,在編輯員工或者編輯菜品分類時(shí)需要設(shè)置修改時(shí)間、修改人等字段。
這些字段屬于公共字段,也就是也就是在我們的系統(tǒng)中很多表中都會(huì)有這些字段,如下:
序號(hào) | 字段名 | 含義 | 數(shù)據(jù)類型 |
---|---|---|---|
1 | create_time | 創(chuàng)建時(shí)間 | datetime |
2 | create_user | 創(chuàng)建人id | bigint |
3 | update_time | 修改時(shí)間 | datetime |
4 | update_user | 修改人id | bigint |
而針對(duì)于這些字段,我們的賦值方式為:
1). 在新增數(shù)據(jù)時(shí), 將createTime、updateTime 設(shè)置為當(dāng)前時(shí)間, createUser、updateUser設(shè)置為當(dāng)前登錄用戶ID。
2). 在更新數(shù)據(jù)時(shí), 將updateTime 設(shè)置為當(dāng)前時(shí)間, updateUser設(shè)置為當(dāng)前登錄用戶ID。
目前,在我們的項(xiàng)目中處理這些字段都是在每一個(gè)業(yè)務(wù)方法中進(jìn)行賦值操作,如下:
新增員工方法:
/** * 新增員工 * * @param employeeDTO */ public void save(EmployeeDTO employeeDTO) { //....................... // //設(shè)置當(dāng)前記錄的創(chuàng)建時(shí)間和修改時(shí)間 employee.setCreateTime(LocalDateTime.now()); employee.setUpdateTime(LocalDateTime.now()); //設(shè)置當(dāng)前記錄創(chuàng)建人id和修改人id employee.setCreateUser(BaseContext.getCurrentId());//目前寫個(gè)假數(shù)據(jù),后期修改 employee.setUpdateUser(BaseContext.getCurrentId()); /// employeeMapper.insert(employee); }
編輯員工方法:
/** * 編輯員工信息 * * @param employeeDTO */ public void update(EmployeeDTO employeeDTO) { //........................................ /// employee.setUpdateTime(LocalDateTime.now()); employee.setUpdateUser(BaseContext.getCurrentId()); /// employeeMapper.update(employee); }
新增菜品分類方法:
/** * 新增分類 * @param categoryDTO */ public void save(CategoryDTO categoryDTO) { //.................................... // //設(shè)置創(chuàng)建時(shí)間、修改時(shí)間、創(chuàng)建人、修改人 category.setCreateTime(LocalDateTime.now()); category.setUpdateTime(LocalDateTime.now()); category.setCreateUser(BaseContext.getCurrentId()); category.setUpdateUser(BaseContext.getCurrentId()); /// categoryMapper.insert(category); }
修改菜品分類方法:
/** * 修改分類 * @param categoryDTO */ public void update(CategoryDTO categoryDTO) { //.................................... // //設(shè)置修改時(shí)間、修改人 category.setUpdateTime(LocalDateTime.now()); category.setUpdateUser(BaseContext.getCurrentId()); // categoryMapper.update(category); }
如果都按照上述的操作方式來處理這些公共字段, 需要在每一個(gè)業(yè)務(wù)方法中進(jìn)行操作, 編碼相對(duì)冗余、繁瑣,那能不能對(duì)于這些公共字段在某個(gè)地方統(tǒng)一處理,來簡化開發(fā)呢?
答案是可以的,我們使用AOP切面編程,實(shí)現(xiàn)功能增強(qiáng),來完成公共字段自動(dòng)填充功能。
1.2 實(shí)現(xiàn)思路
在實(shí)現(xiàn)公共字段自動(dòng)填充,也就是在插入或者更新的時(shí)候?yàn)橹付ㄗ侄钨x予指定的值,使用它的好處就是可以統(tǒng)一對(duì)這些字段進(jìn)行處理,避免了重復(fù)代碼。
在上述的問題分析中,我們提到有四個(gè)公共字段,需要在新增/更新中進(jìn)行賦值操作, 具體情況如下:
序號(hào) | 字段名 | 含義 | 數(shù)據(jù)類型 | 操作類型 |
---|---|---|---|---|
1 | create_time | 創(chuàng)建時(shí)間 | datetime | insert |
2 | create_user | 創(chuàng)建人id | bigint | insert |
3 | update_time | 修改時(shí)間 | datetime | insert、update |
4 | update_user | 修改人id | bigint | insert、update |
實(shí)現(xiàn)步驟:
1). 自定義注解 AutoFill,用于標(biāo)識(shí)需要進(jìn)行公共字段自動(dòng)填充的方法
2). 自定義切面類 AutoFillAspect,統(tǒng)一攔截加入了 AutoFill 注解的方法,通過反射為公共字段賦值
3). 在 Mapper 的方法上加入 AutoFill 注解
若要實(shí)現(xiàn)上述步驟,需掌握以下知識(shí)(之前課程內(nèi)容都學(xué)過)
**技術(shù)點(diǎn):**枚舉、注解、AOP、反射
1.3 代碼開發(fā)
按照上一小節(jié)分析的實(shí)現(xiàn)步驟依次實(shí)現(xiàn),共三步。
1.3.1 步驟一
自定義注解 AutoFill
進(jìn)入到sky-server模塊,創(chuàng)建com.sky.annotation包。
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; /** * 自定義注解,用于標(biāo)識(shí)某個(gè)方法需要進(jìn)行功能字段自動(dòng)填充處理 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface AutoFill { //數(shù)據(jù)庫操作類型:UPDATE INSERT OperationType value(); }
其中OperationType已在sky-common模塊中定義
package com.sky.enumeration; /** * 數(shù)據(jù)庫操作類型 */ public enum OperationType { /** * 更新操作 */ UPDATE, /** * 插入操作 */ INSERT }
1.3.2 步驟二
自定義切面 AutoFillAspect
在sky-server模塊,創(chuàng)建com.sky.aspect包。
package com.sky.aspect; /** * 自定義切面,實(shí)現(xiàn)公共字段自動(dòng)填充處理邏輯 */ @Aspect @Component @Slf4j public class AutoFillAspect { /** * 切入點(diǎn) */ @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)") public void autoFillPointCut(){} /** * 前置通知,在通知中進(jìn)行公共字段的賦值 */ @Before("autoFillPointCut()") public void autoFill(JoinPoint joinPoint){ /重要 //可先進(jìn)行調(diào)試,是否能進(jìn)入該方法 提前在mapper方法添加AutoFill注解 log.info("開始進(jìn)行公共字段自動(dòng)填充..."); } }
完善自定義切面 AutoFillAspect 的 autoFill 方法
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.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.reflect.Method; import java.time.LocalDateTime; /** * 自定義切面,實(shí)現(xiàn)公共字段自動(dòng)填充處理邏輯 */ @Aspect @Component @Slf4j public class AutoFillAspect { /** * 切入點(diǎn) */ @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)") public void autoFillPointCut(){} /** * 前置通知,在通知中進(jìn)行公共字段的賦值 */ @Before("autoFillPointCut()") public void autoFill(JoinPoint joinPoint){ log.info("開始進(jìn)行公共字段自動(dòng)填充..."); //獲取到當(dāng)前被攔截的方法上的數(shù)據(jù)庫操作類型 MethodSignature signature = (MethodSignature) joinPoint.getSignature();//方法簽名對(duì)象 AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);//獲得方法上的注解對(duì)象 OperationType operationType = autoFill.value();//獲得數(shù)據(jù)庫操作類型 //獲取到當(dāng)前被攔截的方法的參數(shù)--實(shí)體對(duì)象 Object[] args = joinPoint.getArgs(); if(args == null || args.length == 0){ return; } Object entity = args[0]; //準(zhǔn)備賦值的數(shù)據(jù) LocalDateTime now = LocalDateTime.now(); Long currentId = BaseContext.getCurrentId(); //根據(jù)當(dāng)前不同的操作類型,為對(duì)應(yīng)的屬性通過反射來賦值 if(operationType == OperationType.INSERT){ //為4個(gè)公共字段賦值 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); //通過反射為對(duì)象屬性賦值 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個(gè)公共字段賦值 try { Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class); Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class); //通過反射為對(duì)象屬性賦值 setUpdateTime.invoke(entity,now); setUpdateUser.invoke(entity,currentId); } catch (Exception e) { e.printStackTrace(); } } } }
1.3.3 步驟三
在Mapper接口的方法上加入 AutoFill 注解
以CategoryMapper為例,分別在新增和修改方法添加@AutoFill()注解,也需要EmployeeMapper做相同操作
package com.sky.mapper; @Mapper public interface CategoryMapper { /** * 插入數(shù)據(jù) * @param category */ @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 insert(Category category); /** * 根據(jù)id修改分類 * @param category */ @AutoFill(value = OperationType.UPDATE) void update(Category category); }
同時(shí),將業(yè)務(wù)層為公共字段賦值的代碼注釋掉。
1). 將員工管理的新增和編輯方法中的公共字段賦值的代碼注釋。
2). 將菜品分類管理的新增和修改方法中的公共字段賦值的代碼注釋。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring相關(guān)知識(shí)點(diǎn)的總結(jié)與梳理
今天小編就為大家分享一篇關(guān)于Spring相關(guān)知識(shí)點(diǎn)的總結(jié)與梳理,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02Java 創(chuàng)建動(dòng)態(tài)類和查看方法列表信息的實(shí)例
這篇文章主要介紹了 Java 創(chuàng)建動(dòng)態(tài)類和查看方法列表信息的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06Java mysql詳細(xì)講解雙數(shù)據(jù)源配置使用
在開發(fā)過程中我們常常會(huì)用到兩個(gè)數(shù)據(jù)庫,一個(gè)數(shù)據(jù)用來實(shí)現(xiàn)一些常規(guī)的增刪改查,另外一個(gè)數(shù)據(jù)庫用來實(shí)時(shí)存數(shù)據(jù)。進(jìn)行數(shù)據(jù)的統(tǒng)計(jì)分析??梢宰x寫分離??梢愿玫膬?yōu)化和提高效率;或者兩個(gè)數(shù)據(jù)存在業(yè)務(wù)分離的時(shí)候也需要多個(gè)數(shù)據(jù)源來實(shí)現(xiàn)2022-06-06SpringBoot整合Spring?Boot?Admin實(shí)現(xiàn)服務(wù)監(jiān)控的方法
這篇文章主要介紹了SpringBoot整合Spring?Boot?Admin實(shí)現(xiàn)服務(wù)監(jiān)控,內(nèi)容包括Server端服務(wù)開發(fā),Client端服務(wù)開發(fā)其中Spring Boot Admin還可以對(duì)其監(jiān)控的服務(wù)提供告警功能,如服務(wù)宕機(jī)時(shí),可以及時(shí)以郵件方式通知運(yùn)維人員,感興趣的朋友跟隨小編一起看看吧2022-03-03Java發(fā)送form-data請(qǐng)求實(shí)現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了Java發(fā)送form-data請(qǐng)求實(shí)現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Java貪心算法之Prime算法原理與實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Java貪心算法之Prime算法原理與實(shí)現(xiàn)方法,簡單描述了Prime算法的概念、原理、實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2017-09-09Java代理模式實(shí)例詳解【靜態(tài)代理與動(dòng)態(tài)代理】
這篇文章主要介紹了Java代理模式,結(jié)合實(shí)例形式詳細(xì)分析了java靜態(tài)代理與動(dòng)態(tài)代理模式相關(guān)概念、原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-09-09