wordpress做論壇網站app推廣方法
文章目錄
-
- 概要
- 整體架構流程
- 技術細節(jié)
- 小結
概要
在新增員工或者新增菜品分類時需要設置創(chuàng)建時間、創(chuàng)建人、修改時間、修改人等字段,在編輯員工或者編輯菜品分類時需要設置修改時間、修改人等字段。這些字段屬于公共字段,也就是也就是在我們的系統(tǒng)中很多表中都會有這些字段,如下:
序號 | 字段名 | 含義 | 數據類型 |
---|---|---|---|
1 | create_time | 創(chuàng)建時間 | datetime |
2 | create_user | 創(chuàng)建人id | bigint |
3 | update_time | 修改時間 | datetime |
4 | update_user | 修改人id | bigint |
而針對于這些字段,我們的賦值方式為:
1). 在新增數據時, 將createTime、updateTime 設置為當前時間, createUser、updateUser設置為當前登錄用戶ID。
2). 在更新數據時, 將updateTime 設置為當前時間, updateUser設置為當前登錄用戶ID。
我們使用AOP切面編程,實現功能增強,來完成公共字段自動填充功能。
技術細節(jié)
在實現公共字段自動填充,也就是在插入或者更新的時候為指定字段賦予指定的值,使用它的好處就是可以統(tǒng)一對這些字段進行處理,避免了重復代碼。在上述的問題分析中,我們提到有四個公共字段,需要在新增/更新中進行賦值操作, 具體情況如下:
序號 | 字段名 | 含義 | 數據類型 | 操作類型 |
---|---|---|---|---|
1 | create_time | 創(chuàng)建時間 | datetime | insert |
2 | create_user | 創(chuàng)建人id | bigint | insert |
3 | update_time | 修改時間 | datetime | insert、update |
4 | update_user | 修改人id | bigint | insert、update |
實現步驟:
1). 自定義注解 AutoFill,用于標識需要進行公共字段自動填充的方法
2). 自定義切面類 AutoFillAspect,統(tǒng)一攔截加入了 AutoFill 注解的方法,通過反射為公共字段賦值
3). 在 Mapper 的方法上加入 AutoFill 注解
若要實現上述步驟,需掌握以下知識(之前課程內容都學過)
技術點:枚舉、注解、AOP、反射
- 枚舉類:
package com.sky.enumeration;/*** 數據庫操作類型*/
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 {//數據庫操作類型:UPDATE INSERTOperationType value();
}
- ?自定義切面類:
- 公共字段填充,我們選擇在連接點(方法)執(zhí)行前就進行,所以用@Before
- 將連接點上的@AutoFill注解攔截下
- 利用注解的.value()獲取到注解中的內容(數據庫操作類型)
- 獲取到該連接點的參數
- 準備好填充的內容
- 利用反射根據數據庫操作類型進行填充
-
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);//獲取數據庫操作類型(INSERT,UPDATE)OperationType operationType = autoFIll.value();//獲取當前被攔截方法上的參數(實體對象)Object[] args = joinPoint.getArgs();if (args == null || args.length == 0) {return;}Object entity = args[0];//準備要填充的內容LocalDateTime now = LocalDateTime.now();Long id = BaseContext.getCurrentId();//根據數據庫操作類型進行賦值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);