• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

pagehelper插件进行分页

武飞扬头像
三十小库里
帮助1

创建项目

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

学新通

第二步:        学新通 

一、 原理概述

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配置

#pageHelper 分页插件的配置 
pagehelper: 
  auto-dialect: true 
  reasonable: true 
  support-methods-arguments: true 
  params: count=countSql

2.3 建立实体类

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

2.4 mapper层 (数据持久层)

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

2.5 service层 (业务逻辑层)

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

2.6 controller层

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

 页面显示

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgghaih
系列文章
更多 icon
同类精品
更多 icon
继续加载