废话不多说,直接上代码吧,具体怎么用看注释
package com.demo.mall.commen.config;
import org.apache.commons.beanutils.BeanUtils;
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.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.Date;
/**
* Mapper切面,插入创建人,创建时间,修改人,修改时间
*
*/
@Aspect
@Component
@Configuration
public class MapperAspect {
private static final String creatorId = "creatorId";
private static final String creator="creator";
private static final String createTime = "createTime";
private static final String updator = "updator";
private static final String updateTime = "updateTime";
@Pointcut("execution(* com.demo.mall.*.mapper.*.update*(..))")//扫描mapper层的方法前缀为update***方法
public void daoUpdate() { //当扫描到执行带有update方法时候,就执行@Around("daoUpdate()")这个注解的方法
}
@Pointcut("execution(* com.demo.mall.*.mapper.*.insert*(..))") //扫描mapper层的方法前缀为insert***方法
public void daoCreate() {
}
@Pointcut("execution(* com.demo.mall.*.mapper.*.delete*(..))")//扫描mapper层的方法前缀为delete***方法
public void daoDelete() {
}
@Around("daoUpdate()")
public Object doAroundUpdate(ProceedingJoinPoint pjp) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes)
RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return pjp.proceed();
}
// HttpServletRequest request = attributes.getRequest();
Object[] objects = pjp.getArgs();
if (objects != null && objects.length > 0){
for (Object arg : objects) {
BeanUtils.setProperty(arg, updator, "updator");
BeanUtils.setProperty(arg, updateTime, new Date());
}
}
Object object = pjp.proceed();
return object;
}
@Around("daoCreate()")
public Object doAroundCreate(ProceedingJoinPoint pjp) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes)
RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return pjp.proceed();
}
//获取切入点的入参
Object[] objects = pjp.getArgs();
if (objects != null && objects.length > 0) {
for (Object arg : objects) {
BeanUtils.setProperty(arg, creatorId, "1");
BeanUtils.setProperty(arg, creator, "user_1");
BeanUtils.setProperty(arg, updator, "user_1");
BeanUtils.setProperty(arg, createTime, new Date());
}
}
Object object = pjp.proceed();
return object;
}
@Around("daoDelete()")
public Object doAroundDelete(ProceedingJoinPoint pjp) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes)
RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return pjp.proceed();
}
//获取切入点的入参
Object[] objects = pjp.getArgs();
if (objects != null && objects.length > 0){
for (Object arg : objects) {
BeanUtils.setProperty(arg, updator, "delete");
BeanUtils.setProperty(arg, updateTime, new Date());
}
}
Object object = pjp.proceed();
return object;
}
}