参考:
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>