pagehelper插件进行分页
短信预约 -IT技能 免费直播动态提醒
创建项目
第一步(完成以下操作进行下一步):
第二步:
一、 原理概述
PageHelper是MyBatis的一个插件,内部实现了一个PageInterceptor拦截器。Mybatis会加载这个拦截器到拦截器链中。在我们使用过程中先使用PageHelper.startPage这样的语句在当前线程上下文中设置一个ThreadLocal变量,再利用PageInterceptor这个分页拦截器拦截,从ThreadLocal中拿到分页的信息,如果有分页信息拼装分页SQL(limit语句等)进行分页查询,最后再把ThreadLocal中的东西清除掉。
二、 springboot+pageHelper带条件分页
2.1 添加依赖
com.github.pagehelper pagehelper-spring-boot-starter 1.4.1
2.2 pageHelper分页插件的yml配置
#pageHelper 分页插件的配置 pagehelper: auto-dialect: true reasonable: true support-methods-arguments: true params: count=countSql
2.3 建立实体类
package com.boot.springboot1223.pojo;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableId;import com.baomidou.mybatisplus.annotation.TableName;import lombok.Data;import java.io.Serializable;import java.util.Date;@Data@TableName("action")public class Action{ @TableId(type = IdType.AUTO) private Integer actionId; private String orderSn; private Integer actionUser; private Integer orderStatus; private Integer payStatus; private Integer shippingStatus; private String actionNote; private String actionTime; private String statusDesc; @TableField(exist = false) private String orderTime;}
2.4 mapper层 (数据持久层)
package com.boot.springboot1223.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.boot.springboot1223.pojo.Action;import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapperpublic interface ActionMapper extends BaseMapper { List findPage(Action action);}
2.5 service层 (业务逻辑层)
package com.boot.springboot1223.service;import com.baomidou.mybatisplus.extension.service.IService;import com.boot.springboot1223.pojo.Action;import com.github.pagehelper.PageInfo;import java.util.List;public interface ActionService extends IService { PageInfo findPage(Action action,Integer pageIndex,Integer pageSize);}
package com.boot.springboot1223.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import com.boot.springboot1223.mapper.ActionMapper;import com.boot.springboot1223.pojo.Action;import com.boot.springboot1223.service.ActionService;import com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class ActionServiceImpl extends ServiceImpl implements ActionService { @Autowired private ActionMapper actionMapper; @Override public PageInfo findPage(Action action, Integer pageIndex, Integer pageSize) { //调用分页插件的工具类 计算总页数 PageHelper.startPage(pageIndex,pageSize); //获取所有数据 List page = actionMapper.findPage(action); //获取所有的数据直接给pageInfo PageInfo pageInfo=new PageInfo(page); return pageInfo; }}
2.6 controller层
package com.boot.springboot1223.controller;import com.boot.springboot1223.pojo.Action;import com.boot.springboot1223.pojo.Order;import com.boot.springboot1223.service.ActionService;import com.boot.springboot1223.service.OrderService;import com.github.pagehelper.PageInfo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;@Controllerpublic class ActionController { @Autowired private ActionService actionService; @RequestMapping("/findPage") public String findPage( Action action, @RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex, @RequestParam(value = "pageSize",defaultValue = "1",required = false) Integer pageSize, Model model ){ PageInfo page = actionService.findPage(action,pageIndex, pageSize); model.addAttribute("path","findPage?pageIndex="); model.addAttribute("page",page); model.addAttribute("action",action); return "list"; }}
页面显示
来源地址:https://blog.csdn.net/qq_57512436/article/details/128446865
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341