需求:
有时候我们在进行某些操作时,可能需要额外进行复制操作,而这些字段往往不是由前端/客户端填写输入的,而是由后端给与,类似于 登陆者、创建时间、更新时间等字段,这时,可以借助AOP指定mapper层(也可改为其它层)进行默认值赋予
package com.taia.yms.aop;
import cn.hutool.core.util.ObjectUtil;
import com.taia.yms.util.ReflectionUtils;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.lang.reflect.*;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 为 某些字段设置默认值,mapper层代码生成不利于做修改,针对配置的字段 及 方法进行动态设值
*/
@Component
@Aspect
@Order(3)
public class MapperDefaultValueAspect {
private static final Logger log = LoggerFactory.getLogger(MapperDefaultValueAspect.class);
//key:方法名 value:字段+默认值
private static final Map<String,Map<String,Object>> MAP = new HashMap<>(10);
//公共字段 map
private static final Map<String,Object> COMMON_COLUMN_VALUE_MAP = new HashMap<>(10);
//指定方法map
private static final Map<String,Object> INSERT_COLUMN_VALUE_MAP = new HashMap<>(10);
static {
//公共字段
COMMON_COLUMN_VALUE_MAP.put("status","1");
COMMON_COLUMN_VALUE_MAP.put("lastUpdatedTime",new Timestamp(System.currentTimeMillis()));
INSERT_COLUMN_VALUE_MAP.put("createdTime",new Timestamp(System.currentTimeMillis()));
INSERT_COLUMN_VALUE_MAP.putAll(COMMON_COLUMN_VALUE_MAP);
MAP.put("insert",INSERT_COLUMN_VALUE_MAP);
MAP.put("updateByPrimaryKey",COMMON_COLUMN_VALUE_MAP);
}
//定义pointcut签名
@Pointcut("execution(* com.taia.yms.mapper.*.*(..))")
private void pointCut() {
//方法为空,仅做签名
}
//对切点方法进行前置增强,就是在调用切点方法前进行做一些必要的操作,这就成为增强
@Before("pointCut()")
public void getRes(JoinPoint joinPoint) throws Throwable {
log.info("开始拦截。。。");
// 获取方法签名
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
// 获取方法名
String currentMethodName = method.getName();
if(!MAP.containsKey(currentMethodName)){
return;
}
Map<String, Object> map = MAP.get(currentMethodName);
if(map == null){
return;
}
Object[] objects = joinPoint.getArgs();
Parameter[] parameters = method.getParameters();
if(parameters == null){
return;
}
for (int j = 0; j < objects.length; j++) {
Object arg = objects[j];
Class<?> aClass = arg.getClass();
List<Field> allFields = ReflectionUtils.getAllFields(aClass);
for (Field field: allFields) {
//设置对象的访问权限,保证对private的属性的访问
field.setAccessible(true);
Object value = field.get(arg);
//设置默认值
if(map.containsKey(field.getName()) && ObjectUtil.isEmpty(value)){
field.set(arg,map.get(field.getName()));
}
}
}
}
}
package com.taia.yms.util;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class ReflectionUtils {
/**
* 获取某个类的所有属性 包含父类
* @param clazz
* @return
*/
public static List<Field> getAllFields(Class<?> clazz) {
List<Field> fieldList = new ArrayList<>();
Class<?> superclass = clazz;
// 循环直到Object类,因为Object类是所有类的根类
while (superclass != null && !Object.class.equals(superclass)) {
// 获取该类声明的所有字段(包括私有字段)
Field[] fields = superclass.getDeclaredFields();
for (Field field : fields) {
fieldList.add(field);
}
// 递归获取父类的字段
superclass = superclass.getSuperclass();
}
return fieldList;
}
}