参考:

https://blog.csdn.net/qq_24076135/article/details/85212081

https://www.jianshu.com/p/036d31ae77d3

一、编写分页实体类

/**
* 此类用于返回分页结果集
* @param <T>
*/
public class PageResult<T> { private Long total;
private Integer totalPage;
private List<T> rows; public PageResult() {
} public PageResult(List<T> rows) {
this.rows = rows;
PageInfo pageInfo = new PageInfo(rows);
this.total = pageInfo.getTotal();
} public PageResult(Long total, List<T> rows) {
this.total = total;
this.rows = rows;
} public PageResult(Long total, Integer totalPage, List<T> rows) {
this.total = total;
this.totalPage = totalPage;
this.rows = rows;
} public Long getTotal() {
return total;
} public void setTotal(Long total) {
this.total = total;
} public Integer getTotalPage() {
return totalPage;
} public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
} public List<T> getRows() {
return rows;
} public void setRows(List<T> rows) {
this.rows = rows;
}
}

二、未使用aop时,编写service层

 public PageResult<Device> selectList(Map<String,Object> map) {
this.page = Integer.parseInt((String) map.get("page"));
this.size = Integer.parseInt((String) map.get("size"));
PageHelper.startPage(this.page,this.size);
List<Device> devices = this.deviceMapper.selectList(map);
PageInfo<Device> pageInfo = new PageInfo<>(devices);
return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
}

使用aop后:

    public PageResult<Device> selectList(Map<String,Object> map) {
List<Device> devices = this.deviceMapper.selectList(map);
return new PageResult<Device>(devices);
}

分离的部分:

        this.page = Integer.parseInt((String) map.get("page"));
this.size = Integer.parseInt((String) map.get("size"));
PageHelper.startPage(this.page,this.size);
     PageInfo<Device> pageInfo = new PageInfo<>(devices);

三、dao层(即mapper)

public interface DeviceMapper {

    public List<Device> selectList(Map<String,Object> map);
}

四、Controller层(有待优化)

    @GetMapping("/list")
public ResponseEntity<PageResult<Device>> selectList(@RequestParam Map<String,Object> map){
PageResult<Device> result = this.deviceService.selectList(map);
if(CollectionUtils.isEmpty(result.getRows())){
result = null;
return ResponseEntity.ok(result);
}
return ResponseEntity.ok(result);
}

五、重点:编写aop切面类

@Component
@Aspect
@Slf4j
public class PageHelperAspect { @Pointcut("execution(* cn.factory.service.impl.*.*(..))")
public void pageFunction(){}; @Around("pageFunction()")
public Object serviceImplAop(ProceedingJoinPoint pjp){
try {
log.info("进入pagehelper aop"); //获取连接点方法运行时的入参列表
Object[] args = pjp.getArgs(); Map<String,Object> map = (Map<String,Object>)args[0]; // 获取连接点的方法签名对象
Signature signature = pjp.getSignature(); // 获取连接点所在的类的对象(实例)
Object target = pjp.getTarget(); PageHelper.startPage(Integer.parseInt((String) map.get("page")),Integer.parseInt((String) map.get("size"))); log.info("方法[{}]开始执行.....",signature.getName());
// 调用业务层方法,执行sql语句
Object object = pjp.proceed();
log.info("方法[{}]执行结束....",signature.getName());
// if (object instanceof List) {
// List objList = (List) object;
// PageInfo pageInfo = new PageInfo<>(objList);
// return pageInfo;
// }
return object;
} catch (Throwable throwable) {
log.info("pageInfo aop执行失败....");
throw new RuntimeException(throwable);
} finally {
log.info("serviceImplAop执行结束.....");
}
}
}

附上所需的依赖:如果依赖版本不匹配或者错误可能会出现net.sf.jsqlparser.statement.select.PlainSelect.getGroupByColumnReferences()Ljava/util/List;] with root cause此类错误

<!-- web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- mybatis的启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- 通用mapper启动器 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</dependency> <!-- mybatis的分页插件 -->
<!--pageHelper基本依赖 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<!-- 不加这两个依赖分页不会成功 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!-- jdbc启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- springboot检测服务启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.19</version>
</dependency> <!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
<!--加密-->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>

springboot2.0 使用aop实现PageHelper分页的更多相关文章

  1. SpringBoot2.0 使用AOP统一处理Web请求日志(完整版)

    一,加入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  2. pageHelper分页

    引入jar包 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pag ...

  3. spring-boot-2.0.3源码篇 - pageHelper分页,绝对有值得你看的地方

    前言 开心一刻 说实话,作为一个宅男,每次被淘宝上的雄性店主追着喊亲,亲,亲,这感觉真是恶心透顶,好像被强吻一样.........更烦的是我每次为了省钱,还得用个女号,跟那些店主说:“哥哥包邮嘛么叽. ...

  4. SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件

    一.Mybatis框架 1.mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获 ...

  5. SpringBoot2.0之五 优雅整合SpringBoot2.0+MyBatis+druid+PageHelper

    上篇文章我们介绍了SpringBoot和MyBatis的整合,可以说非常简单快捷的就搭建了一个web项目,但是在一个真正的企业级项目中,可能我们还需要更多的更加完善的框架才能开始真正的开发,比如连接池 ...

  6. SpringBoot2.0+Mybatis+PageHelper+Redis实现缓存

    1.在maven引入相关的依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactI ...

  7. SpringBoot2.0 基础案例(11):配置AOP切面编程,解决日志记录业务

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.AOP切面编程 1.什么是AOP编程 在软件业,AOP为Asp ...

  8. spring-boot-2.0.3应用篇 - shiro集成

    前言 上一篇:spring-boot-2.0.3源码篇 - 国际化,讲了如何实现国际化,实际上我工作用的模版引擎是freemaker,而不是thymeleaf,不过原理都是相通的. 接着上一篇,这一篇 ...

  9. spring-boot-2.0.3之quartz集成,不是你想的那样哦!

    前言 开心一刻 晚上回家,爸妈正在吵架,见我回来就都不说话了,看见我妈坐在那里瞪着我爸,我就问老爸“你干什么了惹我妈生这么大气?”  我爸说“没有什么啊,倒是你,这么大了还没有媳妇,要是你有媳妇给我们 ...

随机推荐

  1. 原生js - 两种图片懒加载实现原理

    目前图片懒加载的方式主要有两种: 1.利用getBoundingClientRectAPI得到当前元素与视窗的距离来判断 2.利用h5的新API IntersectionObserver 来实现 ge ...

  2. TCP之1460MSS和1448负载

    TCP和1448 1448字节是实际场景下,单个TCP包的实际运载能力.也就是说,实际场景下,上层调用send(1000KB),下层会把这1000KB封装成多个TCP包进行发送.单个TCP包每次打包1 ...

  3. class10_Frame 框架

    最终运行效果图(程序见序号2):   #!/usr/bin/env python # -*- coding:utf-8 -*- # ---------------------------------- ...

  4. Python3 From Zero——{最初的意识:002~字符串和文本}

    一.使用多个界定符分割字符串 字符串.split(',')形式只适用于单一分割符的情况:多分割符同时应用的时候,可使用re.split() >>> line = 'asdf fjdk ...

  5. 20140412 iphone不完美越狱 无限黑屏解决

    1.不完美越狱 工具:爱思助手.cydia 方法: 爱思助手刷6.1.3固件 一键越狱->关机越狱 高手工具->不完全越狱引导 进入手机后,打开cydia,下载insomnia防黑屏插件 ...

  6. The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树

    签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...

  7. 使用neo4j图数据库的import工具导入数据 -方法和注意事项

    背景 最近我在尝试存储知识图谱的过程中,接触到了Neo4j图数据库,这里我摘取了一段Neo4j的简介: Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中.它是一个嵌 ...

  8. Datagrid 的 SelectItem 和 SelectValue 如何区分、DataContext 和 ItemSource 在绑定时该绑哪个?

    1.selecteditem.selectedvalue.selectedvaluepath三个属性 场景: class T { public string A { get; set; } publi ...

  9. 无LoadLibrary获取指定模块基址

    实际上,这块可以写成汇编,然后做远程注入用 方法 1.通过fs:[30h]获取当前进程的_PEB结构 2.通过_PEB的Ldr成员获取_PEB_LDR_DATA结构 3.通过_PEB_LDR_DATA ...

  10. 9款很棒的网页绘制图表JavaScript框架脚本

    推荐9款很棒的可在网页中绘制图表的JavaScript脚本,这些有趣的JS脚本可以帮助你快速方便的绘制图表(线.面.饼.条…),其中包括jQuery.MooTools.Prototype和一些其它的J ...