使用AOP操作数据库,填充公共字段(创建人、修改人、创建时间、修改时间)代码
1、首先导入依赖
<!--spring-事务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2、自定义接口
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
OperationType value();
}
3、公共字段相关常量
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";
public static final String SET_ID = "setId";
public static final String WX_LOGIN = "https://api.weixin.qq.com/sns/jscode2session";
}
4、threadlocal用来获取当前操作人的id
public class BaseContext {
public static ThreadLocal<Long> threadLocal = new ThreadLocal<>();
public static void setCurrentId(Long id) {
threadLocal.set(id);
}
public static Long getCurrentId() {
return threadLocal.get();
}
public static void removeCurrentId() {
threadLocal.remove();
}
}
5、数据库操作类型枚举类
public enum OperationType {
UPDATE,
INSERT
}
6、自定义切面
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
@Pointcut("execution(* com.sky.mapper.*.*(..))&&@annotation(com.sky.annotation.AutoFill)")
public void autoFillPointCut() {
}
@Before("autoFillPointCut()")
public void autoFill(JoinPoint joinPoint) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException, InstantiationException {
Object[] args = joinPoint.getArgs();
Object arg = args[0];
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
AutoFill annotation = signature.getMethod().getAnnotation(AutoFill.class);
OperationType operationType = annotation.value();
LocalDateTime now = LocalDateTime.now();
Long currentId = BaseContext.getCurrentId();
Long snowId = IdUtil.getSnowflake().nextId();
if (operationType == OperationType.INSERT) {
Method setCreateTime = arg.getClass().getMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
Method setUpdateTime = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
Method setCreateUser = arg.getClass().getMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
Method setUpdateUser = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
setCreateTime.invoke(arg, now);
setUpdateTime.invoke(arg, now);
setCreateUser.invoke(arg, currentId);
setUpdateUser.invoke(arg, currentId);
Field idField = arg.getClass().getDeclaredField("id");
idField.setAccessible(true);
Long id = (Long) idField.get(arg);
if (id==null){
idField.set(arg,snowId);
}
} else if (operationType == OperationType.UPDATE) {
Method setUpdateTime = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
Method setUpdateUser = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
setUpdateTime.invoke(arg, now);
setUpdateUser.invoke(arg, currentId);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通