首先引入pom.xml文件配置

    <!-- mybatis -->
<dependency>
 <groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.</version>
</dependency> <!--mybatis分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.</version>
</dependency>

在application.properties中进行配置

#mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.CarManage.dao #pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql

Controller中

@Controller
@RequestMapping(value="/admin")
public class MessageController { @Autowired
private IMessages iMessages; @GetMapping(value="myMessage")
public String messageList(@RequestParam(value = "currentPage", defaultValue = "") int currentPage,
@RequestParam(value = "pageSize", defaultValue = "") int pageSize, HttpServletRequest request){
PageInfo<CarMessage> messageList = iMessages.findItemByPage(currentPage, pageSize);
request.setAttribute("messageList", messageList);
return "myMessage";
}
}

传三个参数,currentPage 当前页码,pageSize每页显示多少数据,HttpServletRequest用来封装数据
使用@RequestParam进行参数设置,value和defaultValue意指当URL直接请求“/admin/myMessage”时,默认传值参数名称是currentPage,默认值是1;默认传值参数名称是pageSize,默认值是5。这次请求是在别的页面跳转此页面时发生。当进行分页请求时,就会真实传入这2个参数。可以发现request中封装进去了PageInfo对象。这是PageHelper中的东西。里面封装了

n多成员变量,在前台直接调用就可以。

public class PageInfo<T> implements Serializable {
private static final long serialVersionUID = 1L;
//当前页
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size; //由于startRow和endRow不常用,这里说个具体的用法
//可以在页面中"显示startRow到endRow 共size条数据" //当前页面第一个元素在数据库中的行号
private int startRow;
//当前页面最后一个元素在数据库中的行号
private int endRow;
//总记录数
private long total;
//总页数
private int pages;
//结果集
private List<T> list; //前一页
private int prePage;
//下一页
private int nextPage; //是否为第一页
private boolean isFirstPage = false;
//是否为最后一页
private boolean isLastPage = false;
//是否有前一页
private boolean hasPreviousPage = false;
//是否有下一页
private boolean hasNextPage = false;
//导航页码数
private int navigatePages;
//所有导航页号
private int[] navigatepageNums;
//导航条上的第一页
private int navigateFirstPage;
//导航条上的最后一页
private int navigateLastPage;

等等东西。很方便使用。下面会介绍怎么使用。

然后再来看Service

@Service
@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
public class MessagesServiceImpl implements IMessages{ @Autowired
private CarMessageMapper carMessageMapper; @Override
public PageInfo<CarMessage> findItemByPage(int currentPage, int pageSize) {
PageHelper.startPage(currentPage, pageSize);
List<CarMessage> allMessages = carMessageMapper.findAll();
PageInfo<CarMessage> pageInfo = new PageInfo<>(allMessages);
return pageInfo;
} }
//设置分页信息保存到threadlocal中
PageHelper.startPage(currentPage, pageSize); //一定要放在查询之前
//紧跟着的第一个select方法会被分页,contryMapper会被PageInterceptor截拦,截拦器会从threadlocal中取出分页信息,把分页信息加到sql语句中,实现了分页查旬
 List<CarMessage> allMessages = carMessageMapper.findAll();  

将list结果集进行pageInfo包裹,返回包裹对象到Controller中。

前天,我使用的是Thymeleaf模板引擎

 <table id="myMessageTable"  data-toggle="table" >
<thead>
<tr>
<th>消息日期</th>
<th>消息内容</th>
<th>消息状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<th:block th:each="messageList : ${messageList.list}">
<tr th:attr="id=${messageList.id}">
<td th:text="${#dates.format(messageList.createtime,'yyyy-MM-dd HH:mm:ss')}" ></td>
<td th:text="${messageList.content}"></td>
<td>
<th:block th:if="${messageList.status == 0}">
<span>未读</span>
</th:block>
<th:block th:if="${messageList.status == 1}">
<span>已读</span>
</th:block>
</td>
<td>-</td>
</tr>
</th:block>
</tbody>
</table>
<div th:replace="common/pagination :: pageNav(${messageList})"></div>

pageInfo中有一个成员变量list,封装了分页查询的结果集,所以在前台直接循环pageInfo中的list即可。
使用th:include或者th:replace引入th:fragment定义的分页按钮片段,一定要在循环外,因为传入的要是pageInfo整个对象,而不是list

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body> <div th:fragment="pageNav(pageInfo)"> <div class="fixed-table-pagination" style="display: block;" >
<div class="pull-left pagination-detail">
<span class="pagination-info">显示第 <span th:text="${pageInfo.startRow}"></span> 到第 <span th:text="${pageInfo.endRow}"></span> 条记录,总共 <span th:text="${pageInfo.total}"></span>条记录</span>
<span class="page-list">每页显示
<span class="btn-group dropup">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span class="page-size" th:text="${pageInfo.pageSize}" ></span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li th:class="${pageInfo.pageSize== 5 }?'active':''"><a th:href="@{?pageSize=5}" ></a></li>
<li th:class="${pageInfo.pageSize== 8 }?'active':''"><a th:href="@{?pageSize=8}" ></a></li>
</ul>
</span>
条记录
</span>
</div>
</div> <div class="dataTables_paginate paging_bootstrap pagination" >
<ul >
<li th:if="${pageInfo.hasPreviousPage}">
<a th:href="@{'?currentPage='+${pageInfo.prePage}}" >
← Previous
</a>
</li> <th:block th:each="nav : ${pageInfo.navigatepageNums}">
<li th:class="${nav==pageInfo.pageNum}?'active':''"><a th:href="@{'?currentPage='+${nav}}" th:text="${nav}"></a>
</li>
</th:block> <th:block th:if="${pageInfo.hasNextPage}">
<li>
<a th:href="@{'?currentPage='+${pageInfo.nextPage}}" >
Next →
</a>
</li>
</th:block> </ul>
</div> </div> </body>
</html>

这个可以作为通用。在这里直接调用pageInfo所有你需要的成员变量即可。很方便。效果如图。

这是别的页面跳转进来的时候,URL访问路径,并无参数。所以以默认方式分页,默认定位在第一页,每页显示5条数据。

点击第二页的时候,观察URL有参数传入。以传入方式显示。

SpringBoot中Mybaties PageHelper插件使用的更多相关文章

  1. Mybatis中使用PageHelper插件进行分页

    分页的场景比较常见,下面主要介绍一下使用PageHelper插件进行分页操作: 一.概述: PageHelper支持对mybatis进行分页操作,项目在github地址: https://github ...

  2. SpringBoot中使用Maven插件,上传docker镜像

    开启docker远程端口 我上一篇里面写了,这里暴露的路径: 18.16.202.95:2375 简单构建 配置pom.xml文件 在properties中增加一行指定远程主机的位置 <prop ...

  3. 在项目中配置PageHelper插件时遇到类型转换异常

    PageHelper是一种常用的分页工具,按照常规方法在mybatis的配置文件中整合它: <?xml version="1.0" encoding="UTF-8& ...

  4. 关于springboot整合配置pagehelper插件的方法

    一,java代码配置法 这种方法个人感觉比较繁琐不是很推荐,而且也不怎么符合springboot的理念,但是胜在也能够用,所以就列起来,万一以后接手的代码是用这种方式的也方便自己维护. 首先引入jar ...

  5. SpringBoot 使用 MyBatis 分页插件 PageHelper 进行分页查询

    前言:本文档使用的是 SpringBoot,如果是 Spring 还需要在 MyBatis 配置 xml 中配置拦截器,并且 PageHelper 是针对 MyBatis 的,MyBatis 的集成不 ...

  6. SpringBoot整合mybatis使用pageHelper插件进行分页操作

    SpringBoot整合mybatis分页操作 SpringBoot整合Mybatis进行分页操作,这里需要使用Mybatis的分页插件:pageHelper, 关于pageHelper的介绍,请查看 ...

  7. SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页

    SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...

  8. springboot结合mybatis使用pageHelper插件进行分页查询

    1.pom相关依赖引入 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...

  9. springboot中的mybatis是如果使用pagehelper的

    springboot中使用其他组件都是基于自动配置的AutoConfiguration配置累的,pagehelper插件也是一样的,通过PageHelperAutoConfiguration的,这个类 ...

随机推荐

  1. Maven 整合 robot framework 进行测试

    1. 在maven pom.xml中先配置robot framework的plugin: <plugin> <!-- integration test runner (robot-f ...

  2. C/C++中创建(带头结点、不带头结点的)单链表

    1.带头结点的单链表(推荐使用带头结点的单链表)(采用尾插法) 了解单链表中节点的构成 从上图可知,节点包含数据域和指针域,因此,在对节点进行定义时,我们可以如下简单形式地定义: /* 定义链表 */ ...

  3. 009-MailUtils工具类模板

    版本一:JavaMail的一个工具类 package ${enclosing_package}; import java.security.GeneralSecurityException; impo ...

  4. 配置vim插件遇到youcompleteme插件问题解决方案

    今天在Opensuse下配置vim 遇到两个问题 配置插件找到一个很好的博客.学到一些有用技巧 http://hahaya.github.io/2013/07/26/use-vundle.html 但 ...

  5. MyBatis Mapper XML 文件 的学习详解

    MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JDBC 代码来比较,你会发现映射文件节省了大约 ...

  6. 基于resteasy,Base64码上传文件

    package com.xgt.controller.bs; import com.xgt.bean.bs.VersionBean; import com.xgt.common.BaseControl ...

  7. 个人总结——Beta版本

    我们这次项目因为种种原因失败了,没办法达到预期的效果,这一点着实让人难过.但是作为一门课程,并不能说因为此次项目的失败就完全一无所获.在不断遇到困难和解决困难中学习,成长.虽然失败,但此次失败的经验教 ...

  8. url模块学习小结

    url模块是node自带的功能强大的url解析库. var url = require("url"); var str = "http://192.168.0.109:8 ...

  9. ElasticSearch基础入门

    1.query查询表达式 Elasticsearch 提供一个丰富灵活的查询语言叫做 查询表达式 , 查询表达式(Query DSL)是一种非常灵活又富有表现力的 查询语言,它支持构建更加复杂和健壮的 ...

  10. apache配置apache server status,监控服务器访问情况

    在apache配置文件中添加开启代码, 1.如果你的Apache配置文件httpd.conf或者extra/httpd-info.conf中有LoadModule status_module modu ...