AOP切面概述
然后/** * 自定义注解用于标识某个方法需要进行功能字段自动填充处理 */ Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) public interface AutoFill { //数据库操作类型UPDATE INSERT OperationType value(); } /** * 公共字段自动填充相关常量 */ public class AutoFillConstant { /** * 实体类中的方法名称 */ public static final String SET_CREATE_TIME setCreateTime; public static final String SET_UPDATE_TIME setUpdateTime; public static final String SET_CREATE_USER setCreateUser; public static final String SET_UPDATE_USER setUpdateUser; }需要切入点需要连接点需要注入等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; Aspect Component Slf4j public class AutoFillAspectCopy { // 切点在哪个类下的那个方法在mapper下的所有类的所有方法 // 匹配所有带 AutoFill 注解的方法 // 注意*.*(..) 中的 (..) 表示匹配任意参数 Pointcut(execution(* com.sky.mapper.*.*(..)) annotation(com.sky.annotation.AutoFill)) public void autoFillGet(){}; // 获取到方法上的参数链接点 // 匹配AutoFillGet()的方法 Before(autoFillGet()) public void beforeCacheable(JoinPoint joinPoint) throws Exception { log.info(即将执行缓存方法); // 获取方法上的注解 MethodSignature signature (MethodSignature) joinPoint.getSignature(); // 获得方法上的注解对象 AutoFill autoFill signature.getMethod().getAnnotation(AutoFill.class); //autoFill的的注解的方法是否包含update/insert?? OperationType autoFillValue autoFill.value(); // 获取到第一个参数 Object[] args joinPoint.getArgs(); if(args null || args.length 0){ return; } // 第一个参数的实体那么就要通过反射机制赋值 Object entity args[0]; LocalDateTime now LocalDateTime.now();// 获取当前的时间 Long currentId BaseContext.getCurrentId();// 获取当前登录人 // 如何理解就是实体类获取到class getClass,然后获取方法因为已经有set,get方法所以可以直接使用getUpdateTime方法 // 实体类里面有 Data,Builder这2个注解, // setDeclaredMethod(),获取到那个指定的方法设置什么参数 String setCreateTime AutoFillConstant.SET_CREATE_TIME; String setCreateUser AutoFillConstant.SET_CREATE_USER; String setUpdateTime AutoFillConstant.SET_UPDATE_TIME; String setUpdateUser AutoFillConstant.SET_UPDATE_USER; // 拿到设置的值然后赋值 Method setCreateTimeClass entity.getClass().getDeclaredMethod(setCreateTime, LocalDateTime.class); Method setCreateUserClass entity.getClass().getDeclaredMethod(setCreateUser, Long.class); Method setUpdateTimeClass entity.getClass().getDeclaredMethod(setUpdateTime, LocalDateTime.class); Method setUpdateUserClass entity.getClass().getDeclaredMethod(setUpdateUser, Long.class); // invoke,调用调用哪个对象 // 当然是当前的对象啊不然是哪个对象entity设置时间 setCreateTimeClass.invoke(entity,now); // 设置当前时间 setCreateUserClass.invoke(entity,BaseContext.getCurrentId());//设置当前的更新人 setUpdateTimeClass.invoke(entity,now); setUpdateUserClass.invoke(entity,BaseContext.getCurrentId()); // 新增还是更新 if(autoFillValue OperationType.INSERT){ // invoke,调用调用哪个对象 // 当然是当前的对象啊不然是哪个对象entity设置时间 setCreateTimeClass.invoke(entity,now); // 设置当前时间 setCreateUserClass.invoke(entity,BaseContext.getCurrentId());//设置当前的更新人 setUpdateTimeClass.invoke(entity,now); setUpdateUserClass.invoke(entity,BaseContext.getCurrentId()); }else if(autoFillValue OperationType.UPDATE){ setUpdateTimeClass.invoke(entity,now); setUpdateUserClass.invoke(entity,BaseContext.getCurrentId()); } } }mapper,怎么使用/** * 插入菜品数据 * * param dish */ AutoFill(value OperationType.INSERT) void insert(Dish dish);搞定了这样就不用每个entity的实体都要进行赋值复杂的set..get..操作这样简化代码的封装顺序