我们需要用到的技术有:枚举、自定义注解、AOP、反射
场景描述:很多时候我们在执行插入和更新操作时,都需要对时间和操作人进行更新,如果每次都在服务层去手动添加,数量多了会显得很繁琐。于是我们就可以给他进行优化,具体操作如下:
首先我们先自定义一个注解:AutoFill,在注解中我们定义一个方法来辨别操作的数据库类型。
然后我们再去定义一个切面类:在切面类里面使用@Brfore注解来给方法执行前自动填充公共属性即可。
在切面类中我们需要的步骤:
1、先获取到当前拦截器拦截方法的数据库操作类型
2、获取到当前被拦截方法的实体类
3、准备赋值数据
4、根据不同的操作使用反射来赋值
代码--自定义注解和自定义的数据库操作类:
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;
/**
* @author 32338
* @date 2024-04-12 10:47:08
* @description 自定义注解类 用于标识某个方法需要进行功能字段的自动填充
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
OperationType value();//数据库操作类型
}
=======================================
package com.sky.enumeration;
/**
* 数据库操作类型
*/
public enum OperationType {
/**
* 更新操作
*/
UPDATE,
/**
* 插入操作
*/
INSERT
}
代码--自定义的切面类:
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.Method;
import java.time.LocalDateTime;
/**
* @author 32338
* @date 2024-04-12 10:52:39
* @description 自定义切面类,实现公共字段填充
*/
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
/**
* @description
***:切入点
**/
@Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
public void autoFillPointCut(){}
/**
* @description
***:前置通知来添加公共字段
**/
@Before("autoFillPointCut()")
public void autoFill(JoinPoint joinPoint){
log.info("开始公共字段自动填充...");
//获取到当前被拦截方法上的数据库操作类型
MethodSignature signature = (MethodSignature) joinPoint.getSignature();//方法签名对象(要转成MethodSignature)
AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);//获取方法类上的注解对象
OperationType operationType = autoFill.value();//获得数据库操作类型
//获取到当前被拦截方法的实体类--实体对象(默认将实体对象放在参数的第一个)
Object[] args = joinPoint.getArgs();
//防止空指针异常:先判空
if (args.length==0 || args == null){return;}
Object entity = args[0];
//准备赋值数据
LocalDateTime now = LocalDateTime.now();
Long currentId = 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,currentId);
setUpdateTime.invoke(entity,now);
setUpdateUser.invoke(entity,currentId);
} 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,currentId);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}