AOP + PageHelper 无侵入完成分页

梗概

  • @EnablePaging注解开启分页,获取分页条件,返回分页对象
  • @ExecutePaging在DAO层执行分页查询
  • 整个过程对逻辑代码无入侵,极低耦合,完成分页

实现

/**
 * 执行分页,
 * 从线程变量中获取分页pageNum与pageSize  执行分页PageHelper.startPage(pageNumInLocal.get(), pageSizeInLocal.get());
 * 将执行后PageInfo信息封入线程变量
 * @author cheemin
 * @create 2019/1/21
 */
@Target({ElementType.PARAMETER,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExecutePaging {}
/**
 * 启用分页,
 * 从注解处参数内获取分页pageNum与pageSize  存入线程变量
 *
 * 从线程变量中取出PageInfo信息
 * 将注解处返回值的data的list封入PageInfo
 * @author cheemin
 * @create 2019/1/21
 */
@Target({ElementType.PARAMETER,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnablePaging {}
package com.qihoo.datacenter.portal.handler;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qihoo.datacenter.portal.utils.AjaxResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.CodeSignature;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Cheemin
 * @create 2019/1/21
 */
@Aspect
@Component
public class PageHandler {

    private static ThreadLocal pageNumInLocal =new ThreadLocal<>();
    private static ThreadLocal pageSizeInLocal =new ThreadLocal<>();
    private static ThreadLocal pageInfoThreadLocal=new ThreadLocal<>();
    public static int defaultPageNum=1;
    public static int defaultPageSize=12;

    @Pointcut("@annotation(com.qihoo.datacenter.portal.handler.page.EnablePaging)")
    public void enablePagingAspect(){}
    @Pointcut("@annotation(com.qihoo.datacenter.portal.handler.page.ExecutePaging)")
    public void executePagingAspect(){}


    /**
     * 获取pageNum 与 pageSize
     * 将返回值的data取出替换为pageInfo
     * @param point
     * @return
     * @throws Throwable
     */
    @Around(value = "enablePagingAspect()")
    public AjaxResult enablePage(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        String[] paramNames = ((CodeSignature) point.getSignature()).getParameterNames();
        for (int i = 0; i < paramNames.length; i++) {
            if (paramNames[i].equals("pageNum")){
                defaultPageNum= (int)args[i];

            }
            if (paramNames[i].equals("pageSize")){
                defaultPageSize=(int)args[i];
            }
        }
        pageNumInLocal.set(defaultPageNum);
        pageSizeInLocal.set(defaultPageSize);
        Result proceed =(Result) point.proceed();
        if (proceed.isSuccess()){
            PageInfo pageInfo = pageInfoThreadLocal.get();
            List data = (List)proceed.getData();
            pageInfo.setList(data);
            proceed.setData(pageInfo);
        }
        return proceed;
    }

    /**
     * 在dao层执行分页
     * @param point
     * @return
     * @throws Throwable
     */
    @Around(value = "executePagingAspect()")
    public Object  executePage(ProceedingJoinPoint point) throws Throwable {
        PageHelper.startPage(pageNumInLocal.get(), pageSizeInLocal.get());
        Object proceed = point.proceed();
        List data =(List) proceed;
        PageInfo pageInfo = new PageInfo<>(data);
        pageInfoThreadLocal.set(pageInfo);
        return proceed;
    }

}

你可能感兴趣的:(AOP + PageHelper 无侵入完成分页)