pagehelper插件进行分页

待我称王封你为后i 2023-09-27 13:32 214阅读 0赞

创建项目

第一步(完成以下操作进行下一步):

6e03539dfd7a45239493436b48cd60dc.png

" class="reference-link">第二步: 1d44e6cc8dbc4f92b6551a746f811ae7.png

一、 原理概述

PageHelper是MyBatis的一个插件,内部实现了一个PageInterceptor拦截器。Mybatis会加载这个拦截器到拦截器链中。在我们使用过程中先使用PageHelper.startPage这样的语句在当前线程上下文中设置一个ThreadLocal变量,再利用PageInterceptor这个分页拦截器拦截,从ThreadLocal中拿到分页的信息,如果有分页信息拼装分页SQL(limit语句等)进行分页查询,最后再把ThreadLocal中的东西清除掉。

二、 springboot+pageHelper带条件分页

2.1 添加依赖

  1. <dependency>
  2. <groupId>com.github.pagehelper</groupId>
  3. <artifactId>pagehelper-spring-boot-starter</artifactId>
  4. <version>1.4.1</version>
  5. </dependency>

2.2 pageHelper分页插件的yml配置

  1. #pageHelper 分页插件的配置
  2. pagehelper:
  3. auto-dialect: true
  4. reasonable: true
  5. support-methods-arguments: true
  6. params: count=countSql

2.3 建立实体类

  1. package com.boot.springboot1223.pojo;
  2. import com.baomidou.mybatisplus.annotation.IdType;
  3. import com.baomidou.mybatisplus.annotation.TableField;
  4. import com.baomidou.mybatisplus.annotation.TableId;
  5. import com.baomidou.mybatisplus.annotation.TableName;
  6. import lombok.Data;
  7. import java.io.Serializable;
  8. import java.util.Date;
  9. /**
  10. *
  11. * @TableName action
  12. */
  13. @Data
  14. @TableName("action")
  15. public class Action{
  16. /**
  17. * 操作ID
  18. */
  19. @TableId(type = IdType.AUTO)
  20. private Integer actionId;
  21. /**
  22. * 订单编号
  23. */
  24. private String orderSn;
  25. /**
  26. * 操作人
  27. */
  28. private Integer actionUser;
  29. /**
  30. * 订单状态
  31. */
  32. private Integer orderStatus;
  33. /**
  34. * 支付状态
  35. */
  36. private Integer payStatus;
  37. /**
  38. * 配送状态
  39. */
  40. private Integer shippingStatus;
  41. /**
  42. * 操作记录
  43. */
  44. private String actionNote;
  45. /**
  46. * 操作时间
  47. */
  48. private String actionTime;
  49. /**
  50. * 状态描述
  51. */
  52. private String statusDesc;
  53. /**
  54. * 下单时间
  55. */
  56. @TableField(exist = false)
  57. private String orderTime;
  58. }

2.4 mapper层 (数据持久层)

  1. package com.boot.springboot1223.mapper;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.boot.springboot1223.pojo.Action;
  4. import org.apache.ibatis.annotations.Mapper;
  5. import java.util.List;
  6. @Mapper
  7. public interface ActionMapper extends BaseMapper<Action> {
  8. /**
  9. * 分页加模糊查询
  10. * @param action
  11. * @return
  12. */
  13. List<Action> findPage(Action action);
  14. }

2.5 service层 (业务逻辑层)

  1. package com.boot.springboot1223.service;
  2. import com.baomidou.mybatisplus.extension.service.IService;
  3. import com.boot.springboot1223.pojo.Action;
  4. import com.github.pagehelper.PageInfo;
  5. import java.util.List;
  6. public interface ActionService extends IService<Action> {
  7. PageInfo<Action> findPage(Action action,Integer pageIndex,Integer pageSize);
  8. }
  9. package com.boot.springboot1223.service.impl;
  10. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  11. import com.boot.springboot1223.mapper.ActionMapper;
  12. import com.boot.springboot1223.pojo.Action;
  13. import com.boot.springboot1223.service.ActionService;
  14. import com.github.pagehelper.PageHelper;
  15. import com.github.pagehelper.PageInfo;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Service;
  18. import java.util.List;
  19. /**
  20. * @Author
  21. * @Date 2022/12/23
  22. * @Description 类功能描述
  23. */
  24. @Service
  25. public class ActionServiceImpl extends ServiceImpl<ActionMapper, Action> implements ActionService {
  26. @Autowired
  27. private ActionMapper actionMapper;
  28. @Override
  29. public PageInfo<Action> findPage(Action action, Integer pageIndex, Integer pageSize) {
  30. //调用分页插件的工具类 计算总页数
  31. PageHelper.startPage(pageIndex,pageSize);
  32. //获取所有数据
  33. List<Action> page = actionMapper.findPage(action);
  34. //获取所有的数据直接给pageInfo
  35. PageInfo pageInfo=new PageInfo(page);
  36. return pageInfo;
  37. }
  38. }

2.6 controller层

  1. package com.boot.springboot1223.controller;
  2. import com.boot.springboot1223.pojo.Action;
  3. import com.boot.springboot1223.pojo.Order;
  4. import com.boot.springboot1223.service.ActionService;
  5. import com.boot.springboot1223.service.OrderService;
  6. import com.github.pagehelper.PageInfo;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.ui.Model;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestParam;
  12. /**
  13. * @Author
  14. * @Date 2022/12/23
  15. * @Description 类功能描述
  16. */
  17. @Controller
  18. public class ActionController {
  19. @Autowired
  20. private ActionService actionService;
  21. @RequestMapping("/findPage")
  22. public String findPage(
  23. Action action,
  24. @RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
  25. @RequestParam(value = "pageSize",defaultValue = "1",required = false) Integer pageSize,
  26. Model model
  27. ){
  28. PageInfo<Action> page = actionService.findPage(action,pageIndex, pageSize);
  29. model.addAttribute("path","findPage?pageIndex=");
  30. model.addAttribute("page",page);
  31. model.addAttribute("action",action);
  32. return "list";
  33. }
  34. }

页面显示

90b88e21475b41e3b35448b451e5c88e.png

发表评论

表情:
评论列表 (有 0 条评论,214人围观)

还没有评论,来说两句吧...

相关阅读