35 lines
1.1 KiB
Java
35 lines
1.1 KiB
Java
|
|
package com.rzyc.advice;
|
|||
|
|
|
|||
|
|
import com.github.pagehelper.PageHelper;
|
|||
|
|
import org.aspectj.lang.JoinPoint;
|
|||
|
|
import org.aspectj.lang.annotation.Aspect;
|
|||
|
|
import org.aspectj.lang.annotation.Before;
|
|||
|
|
import org.aspectj.lang.annotation.Pointcut;
|
|||
|
|
import org.springframework.stereotype.Component;
|
|||
|
|
|
|||
|
|
import java.lang.reflect.Field;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @Author jilin
|
|||
|
|
* @Date 2021/11/09 11:57
|
|||
|
|
* 分页aop
|
|||
|
|
* 注解写在mapper上,aop代理方式需为jdk代理,cglib代理无法aop mapper
|
|||
|
|
**/
|
|||
|
|
@Aspect
|
|||
|
|
@Component
|
|||
|
|
public class PageAspect {
|
|||
|
|
@Pointcut("@annotation(com.rzyc.advice.PageOperation)execution(* com.rzyc..*.*(..))")
|
|||
|
|
public void page() {}
|
|||
|
|
|
|||
|
|
@Before("page()")
|
|||
|
|
public void pageOperation(JoinPoint joinPoint) throws IllegalAccessException, NoSuchFieldException {
|
|||
|
|
Object[] args = joinPoint.getArgs();
|
|||
|
|
Class clazz = args[0].getClass();
|
|||
|
|
Field page = clazz.getDeclaredField("page");
|
|||
|
|
page.setAccessible(true);
|
|||
|
|
Field pageSize = clazz.getDeclaredField("pageSize");
|
|||
|
|
pageSize.setAccessible(true);
|
|||
|
|
PageHelper.startPage((Integer) page.get(args[0]),(Integer) pageSize.get(args[0]));
|
|||
|
|
}
|
|||
|
|
}
|