最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java Aop實現(xiàn)自動填充字段值示例

 更新時間:2023年09月12日 10:06:54   作者:留胡子的餅干  
這篇文章主要為大家介紹了Aop實現(xiàn)自動填充字段值示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

需求

自動填充更新時間創(chuàng)建時間,創(chuàng)建用戶和更新用戶。

自定義注解

OperationType類是一個枚舉

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill{
    OperationType value();
}

OperationType枚舉類

/**
 * 數(shù)據(jù)庫操作類型
 */
    public enum OperationType {
/**
 * 更新操作
 */
    UPDATE,
/**
 * 插入操作
 */
    INSERT
}

使用aop并且聲明一個事務

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.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Objects;
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
@Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
public void pt() {
}
@Before("pt()")
public void autoFill(JoinPoint joinPoint) {
    //獲取簽名對象
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    //獲取方法上注解
    AutoFill annotation = signature.getMethod().getAnnotation(AutoFill.class);
    OperationType value = annotation.value();
    //解析注解是增加還是更新
    Object[] args = joinPoint.getArgs();
    if (Objects.isNull(args) || args.length == 0) {
        return;
    }
    Object target = args[0];
    LocalDateTime now = LocalDateTime.now();
    Long currentId = BaseContext.getCurrentId();
    Class<?> clazz = target.getClass();
    if (value == OperationType.INSERT) {
        try {
            Method setCreateTimeMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
            Method setCreateUserMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
            setCreateTimeMethod.invoke(target,now);
            setCreateUserMethod.invoke(target,currentId);
            autoFillUpdateMethod(now,currentId,clazz,target);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if (value == OperationType.UPDATE) {
        try {
            autoFillUpdateMethod(now,currentId,clazz,target);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
private void autoFillUpdateMethod(LocalDateTime now, Long id, Class<?> clazz, Object target) throws Exception {
    Method setUpdateTimeMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
    Method setUpdateUserMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
    setUpdateTimeMethod.invoke(target,now);
    setUpdateUserMethod.invoke(target,id);
}

以上就是Aop實現(xiàn)自動填充字段值的詳細內(nèi)容,更多關于Aop實現(xiàn)自動填充字段值的資料請關注腳本之家其它相關文章!

相關文章

最新評論

波密县| 西峡县| 军事| 潜江市| 昭苏县| 灵川县| 芜湖市| 和龙市| 呼和浩特市| 灵台县| 呼和浩特市| 清水县| 巍山| 八宿县| 普洱| 红原县| 德江县| 邻水| 松潘县| 江门市| 中方县| 奎屯市| 桐梓县| 柳江县| 会宁县| 靖边县| 伊川县| 尼玛县| 鹰潭市| 静安区| 肇庆市| 遂川县| 马公市| 澜沧| 砀山县| 淮南市| 义马市| 陵水| 遵化市| 抚顺县| 江北区|