话不多说,直接上代码
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Validator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxuwz.common.AjaxResult;
import com.gxuwz.utils.ServletUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.util.List;
import static com.gxuwz.common.Constants.PAGE_NUM;
import static com.gxuwz.common.Constants.PAGE_SIZE;
@Component
@Aspect
public class PageAspect {
@Pointcut("@annotation(com.gxuwz.annotation.EnablePaging)")
public void annotationPointcut() {
}
@Around("annotationPointcut()")
public AjaxResult doAround(ProceedingJoinPoint joinPoint) throws Throwable {
Integer pageNum = ServletUtils.getParameterToInt(PAGE_NUM);
Integer pageSize = ServletUtils.getParameterToInt(PAGE_SIZE);
if (Validator.isNotNull(pageNum) && Validator.isNotNull(pageSize)) {
Page<?> objectPage = PageHelper.startPage(pageNum, pageSize).setReasonable(true);
AjaxResult ajax = (AjaxResult) joinPoint.proceed();
ajax.put("total",objectPage.getTotal());
return ajax;
}
return (AjaxResult) joinPoint.proceed();
}
@AfterReturning(value = "annotationPointcut()", returning = "keys")
public void doAfterReturningAdvice1(JoinPoint joinPoint, AjaxResult keys) {
keys.put("hhh","哈哈哈");
}
}
ps:这里自定义注解在controller层,要注意spring-mvc.xml要加上<aop:aspectj-autoproxy/>
配置aspectj自动代理,不然会进不去切面