mybatis的分页插件

分页插件主要是PageHelper,去官网复制的配置文件如下,选择其中一个即可,我选择的是第二个

Config PageHelper

1. Using in mybatis-config.xml

<!-- 
    In the configuration file, 
    plugins location must meet the requirements as the following order:
    properties?, settings?, 
    typeAliases?, typeHandlers?, 
    objectFactory?,objectWrapperFactory?, 
    plugins?, 
    environments?, databaseIdProvider?, mappers?
-->
<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- config params as the following -->
        <property name="param1" value="value1"/>
    </plugin>
</plugins>
2. Using in Spring application.xml

config org.mybatis.spring.SqlSessionFactoryBean as following:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <!-- other configuration -->
  <property name="plugins">
    <array>
      <bean class="com.github.pagehelper.PageInterceptor">
        <property name="properties">
          <!-- config params as the following -->
          <value>
            param1=value1
          </value>
        </property>
      </bean>
    </array>
  </property>
</bean>

service下分页方法


@Autowired
private StudentMapper studentMapper;
public PageInfo<Student> findByPage(Integer pageNum, Integer pageSize) {
    if (pageNum == null){
        pageNum = 1;
    }
    PageHelper.startPage(pageNum,pageSize);
    List<Student> students = studentMapper.findAll();
    PageInfo<Student> pageInfo = new PageInfo<Student>(students);
    return pageInfo;
}
mapper(dao层)下

```java
List<Student> findAll();
mapper.xml下

```java
<select id="findAll" resultType="Student" >
    select * from student
</select>

Controller里面的分页方法

//将页面展示在客服端,并分页
 @RequestMapping("/students")
 public String findByPage(Model model, Integer pageNum){
     PageInfo<Student> pageInfo = studentService.findByPage(pageNum,3);
     model.addAttribute("pageInfo",pageInfo);
     return "index";
 }

前台index.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: 
  Date: 2019/11/7
  Time: 11:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body >
        <div align="center">
        <a href="${pageContext.request.contextPath}/edit">增加学生信息</a>
        <table  border="1" cellpadding="20" cellspacing="0" >
            <tr>
                <th>编号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>年级</th>
                <th>操作</th>
            </tr>
            <c:forEach items="${pageInfo.list}" var="student">
                <tr>

                    <th>${student.id}</th>
                    <th>${student.name}</th>
                    <th>${student.sex}</th>
                    <th>${student.gradeId}</th>
                    <th>
                        <a href="edit?id=${student.id}" >编辑</a>
                        <a href="${pageContext.request.contextPath}/delete?id=${student.id}" >删除</a>
                    </th>

                </tr>
            </c:forEach>
        </table>
        <span>第${pageInfo.pageNum}/ 共${pageInfo.pages}</span>
        <a href="students?pageNum=1" >首页</a>


        <c:if test="${pageInfo.hasPreviousPage}">
            <a href="students?pageNum=${pageInfo.pageNum-1}" >上一页</a>
        </c:if>
        <c:if test="${pageInfo.hasNextPage}">
            <a href="students?pageNum=${pageInfo.pageNum+1}" >下一页</a>
        </c:if>

        <a href="students?pageNum=${pageInfo.pages}" >尾页</a>
        </div>
</body>
</html>

运行结果
在这里插入图片描述
总结踩过的坑
由于没有复制分页的配置文件,导致客户端数据没分页,开始我以为是控制层的数据没有传过去,疯狂的修改控制层代码
然而并未有什么卵用,
报的错好像是mybatissystem什么什么exception
记住一定要加分页的配置文件!!!(如果用mybatis自带的分页功能的话)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值