栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

自定义注解结合pageHelper实现分页

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自定义注解结合pageHelper实现分页

话不多说,直接上代码
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)) {
            // 设置分页
            PageHelper.startPage(pageNum, pageSize).setReasonable(true);
        }
        AjaxResult ajax = (AjaxResult) joinPoint.proceed();
        ajax.put("total",new PageInfo<>((List) ajax.get("data")).getTotal());
        return ajax;
    }


    
    @AfterReturning(value = "annotationPointcut()", returning = "keys")
    public void doAfterReturningAdvice1(JoinPoint joinPoint, AjaxResult keys) {
        keys.put("hhh","哈哈哈");
    }
}
 
ps:这里自定义注解在controller层,要注意spring-mvc.xml要加上配置aspectj自动代理,不然会进不去切面

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号