wordpress做論壇網(wǎng)站app推廣方法
文章目錄
-
- 概要
- 整體架構(gòu)流程
- 技術(shù)細(xì)節(jié)
- 小結(jié)
概要
在新增員工或者新增菜品分類(lèi)時(shí)需要設(shè)置創(chuàng)建時(shí)間、創(chuàng)建人、修改時(shí)間、修改人等字段,在編輯員工或者編輯菜品分類(lèi)時(shí)需要設(shè)置修改時(shí)間、修改人等字段。這些字段屬于公共字段,也就是也就是在我們的系統(tǒng)中很多表中都會(huì)有這些字段,如下:
序號(hào) | 字段名 | 含義 | 數(shù)據(jù)類(lèi)型 |
---|---|---|---|
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。
我們使用AOP切面編程,實(shí)現(xiàn)功能增強(qiáng),來(lái)完成公共字段自動(dòng)填充功能。
技術(shù)細(xì)節(jié)
在實(shí)現(xiàn)公共字段自動(dòng)填充,也就是在插入或者更新的時(shí)候?yàn)橹付ㄗ侄钨x予指定的值,使用它的好處就是可以統(tǒng)一對(duì)這些字段進(jìn)行處理,避免了重復(fù)代碼。在上述的問(wèn)題分析中,我們提到有四個(gè)公共字段,需要在新增/更新中進(jìn)行賦值操作, 具體情況如下:
序號(hào) | 字段名 | 含義 | 數(shù)據(jù)類(lèi)型 | 操作類(lèi)型 |
---|---|---|---|---|
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). 自定義切面類(lèi) AutoFillAspect,統(tǒng)一攔截加入了 AutoFill 注解的方法,通過(guò)反射為公共字段賦值
3). 在 Mapper 的方法上加入 AutoFill 注解
若要實(shí)現(xiàn)上述步驟,需掌握以下知識(shí)(之前課程內(nèi)容都學(xué)過(guò))
技術(shù)點(diǎn):枚舉、注解、AOP、反射
- 枚舉類(lèi):
package com.sky.enumeration;/*** 數(shù)據(jù)庫(kù)操作類(lèi)型*/
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;/*** 自定義注解,用于標(biāo)識(shí)某個(gè)方法需要進(jìn)行功能字段自動(dòng)填充處理*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {//數(shù)據(jù)庫(kù)操作類(lèi)型:UPDATE INSERTOperationType value();
}
- ?自定義切面類(lèi):
- 公共字段填充,我們選擇在連接點(diǎn)(方法)執(zhí)行前就進(jìn)行,所以用@Before
- 將連接點(diǎn)上的@AutoFill注解攔截下
- 利用注解的.value()獲取到注解中的內(nèi)容(數(shù)據(jù)庫(kù)操作類(lèi)型)
- 獲取到該連接點(diǎn)的參數(shù)
- 準(zhǔn)備好填充的內(nèi)容
- 利用反射根據(jù)數(shù)據(jù)庫(kù)操作類(lèi)型進(jìn)行填充
-
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 {//切入點(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)前被攔截方法上的注解AutoFIll autoFIll = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(AutoFIll.class);//MethodSignature signature = (MethodSignature) joinPoint.getSignature();//方法簽名對(duì)象//AutoFIll autoFill = signature.getMethod().getAnnotation(AutoFIll.class);//獲取數(shù)據(jù)庫(kù)操作類(lèi)型(INSERT,UPDATE)OperationType operationType = autoFIll.value();//獲取當(dāng)前被攔截方法上的參數(shù)(實(shí)體對(duì)象)Object[] args = joinPoint.getArgs();if (args == null || args.length == 0) {return;}Object entity = args[0];//準(zhǔn)備要填充的內(nèi)容LocalDateTime now = LocalDateTime.now();Long id = BaseContext.getCurrentId();//根據(jù)數(shù)據(jù)庫(kù)操作類(lèi)型進(jìn)行賦值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);