首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Spring AOP + PageHelper分页

Spring AOP + PageHelper分页

作者头像
Erwin
发布2019-12-31 12:22:40
发布2019-12-31 12:22:40
8890
举报
文章被收录于专栏:啸天"s blog啸天"s blog
  1. 增加依赖配置 <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.11</version> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.11</version>

增加pagehelper

代码语言:javascript
复制
<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper</artifactId>
  <version>4.1.0</version>
</dependency>
  1. 增加配置 <context:component-scan base-package="com.example.controller"/> <!-- 启动对@AspectJ注解的支持 --> <aop:aspectj-autoproxy/> <!--启动springmvc注解--> <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
  2. 增加注解 @Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface EnablePaging { String value() default ""; }
  3. 增加AOP文件。

这里约定最后两个参数是pageNum 和pageSize

代码语言:javascript
复制
@Aspect
@Component
@Slf4j
public class PageAop {
 
   @Pointcut("@annotation(com.example.annotation.EnablePaging)")
    public void serviceAspect(){
        log.info("serviceAspect");
    }
 
    @Before("serviceAspect()")
    public  void doBefore(JoinPoint joinPoint) {
        log.info("doBefore");
    }
 
 
 
    @Around(value = "serviceAspect()")
    public Object doAround(ProceedingJoinPoint point) throws  Throwable{
        log.info("doAround ");
        Object[] args = point.getArgs();
        Integer pageNum = 1;
        Integer pageSize = 10;
        if(args.length >= 2){
            pageNum = (Integer)args[args.length -2];
            pageSize = (Integer)args[args.length - 1];
        }
        PageHelper.startPage(pageNum, pageSize);
        return  point.proceed(args);
    }
 
 
}
  1. Controller层 @RequestMapping(value = "queryLogs") @EnablePaging @ResponseBody public ServerResponse<PageInfo> queryLogs(HttpServletResponse response, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){ List<Log> list = iLogService.queryList(pageNum, pageSize); PageInfo pageInfo = new PageInfo(list); return ServerResponse.createBySuccess(pageInfo); }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-08-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档