欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis的分页插件

程序员文章站 2024-03-24 16:32:22
...

分页插件主要是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>

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