创建项目
第一步(完成以下操作进行下一步):
第二步:
一、 原理概述
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