如图所示 通用字段 `create_user` bigint(64) DEFAULT NULL COMMENT '创建人', `create_dept` bigint(64) DEFAULT NULL COMMENT '创建部门', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_user` bigint(64) DEFAULT NULL COMMENT '修改人', `update_time` datetime DEFAULT NULL COMMENT '修改时间', `status` int(2) DEFAULT NULL COMMENT '状态', `is_deleted` int(2) DEFAULT '0' COMMENT '是否已删除',
import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.io.Serializable; import java.util.Date; /** *通用实体 */ @Data public class BasePojo implements Serializable { private static final long serialVersionUID = 1L; /** * 创建部门 */ private Long createDept; /** * 创建时间 */ private Date createTime; /** * 修改人 */ private Long updateUser; /** * 修改时间 */ private Date updateTime; private Long createUser; }
自定义注解
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface UserSaveOrUpdate { public static final String INSERT = "insert"; public static final String UPDATE = "update"; String value(); }
import com.ch.event.bo.BasePojo; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springblade.core.secure.utils.AuthUtil; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.Date; @Aspect @Component public class UserSaveOrUpdateAspet { @Pointcut("@annotation(com.ch.event.aop.UserSaveOrUpdate)") public void methodPointcut() { } @Around(value= "methodPointcut()") public Object around(ProceedingJoinPoint point) throws Throwable{ MethodSignature signature = (MethodSignature) point.getSignature(); Method method = signature.getMethod(); //获取被拦截的方法 UserSaveOrUpdate annotation = method.getAnnotation(UserSaveOrUpdate.class);//获取注解 String value = annotation.value();//获取注解的值 String deptId = AuthUtil.getDeptId();//token工具类 取出对应的 数据 Long userId = AuthUtil.getUserId(); Date date = new Date(); if (value.equals(UserSaveOrUpdate.INSERT)){//判断注解的值 如果是新增那就 插多一点 Object[] args = point.getArgs();//获取请求的参数 for (Object arg : args) { if(arg instanceof BasePojo){ BasePojo cmEvent = (BasePojo)arg; cmEvent.setCreateUser(userId); cmEvent.setUpdateUser(userId); cmEvent.setCreateTime(date); cmEvent.setUpdateTime(date); cmEvent.setCreateDept(Long.valueOf(deptId)); } } } return point.proceed();//放行 } }
@PostMapping("/save") @ApiOperationSupport(order = 4) @ApiOperation(value = "新增", notes = "传入cmEvent") @UserSaveOrUpdate(UserSaveOrUpdate.INSERT) public R save(@Valid @RequestBody CmEvent cmEvent) { System.out.println("cmEvent:"+ cmEvent.getCreateUser()+cmEvent.getCreateTime()); return R.status(cmEventService.save(cmEvent)); }