在Springboot与在SSM项目中PageHelper用法及配置

1.导包

    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>4.1.6</version>
    </dependency>

Springboot项目的配置方法:

2.配置文件:PageHelperConfig

package com.how2java.themeleaf.config;

import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class PageHelperConfig {

    @Bean
    public PageHelper pageHelper(){
        PageHelper pageHelper=new PageHelper();
        Properties p=new Properties();
        p.setProperty("offsetAsPageNum","true");//offsetAsPageNum:设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用.
        p.setProperty("rowBoundsWithCount","true");//rowBoundsWithCount:设置为true时,使用RowBounds分页会进行count查询.
        p.setProperty("reasonable","true");//reasonable:启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页。
        pageHelper.setProperties(p);
        return pageHelper;
    }
}

3.CategoryController

package com.how2java.themeleaf.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.how2java.themeleaf.mapper.CategoryMapper;
import com.how2java.themeleaf.pojo.Category;
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;

import java.util.List;

@Controller
public class CategoryController {
    @Autowired
    CategoryMapper categoryMapper;

    @RequestMapping("/listCategory")
    public String listCategory(Model m, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
        PageHelper.startPage(start, size, "id desc");
        List<Category> categoryList = categoryMapper.findAll();
        PageInfo<Category> page = new PageInfo<>(categoryList);


        System.out.println("当前页: " + page.getPageNum());
        System.out.println("总页数:" + page.getPages());
        System.out.println("当前页的记录数:" + page.getSize());

        m.addAttribute("page", page);
        return "listCategory";
    }
    }

4.前台代码:listCategory.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>listCategory</title>
</head>
<body>
<div style="width: 500px;margin: 20px auto;text-align: center">
    <table align="center" border="1" cellspacing="0">
        <tr>
            <td>id</td>
            <td>name</td>
            <td>编辑</td>
            <td>删除</td>
        </tr>
        <tr th:each="c:${page.list}">
            <td th:text="${c.id}"></td>
            <td th:text="${c.name}"></td>
            <td><a th:href="@{editCategory(id=${c.id})}">编辑</a></td>
            <td><a th:href="@{deleteCategory(id=${c.id})}">删除</a></td>
        </tr>
    </table>
    <br/>
    <div>
        <a th:href="@{/listCategory(start=1)}">[首页]</a>
        <a th:href="@{/listCategory(start=${page.getPageNum()-1})}">[上一页]</a>
        <a th:href="@{/listCategory(start=${page.getPageNum()+1})}">[下一页]</a>
        <a th:href="@{/listCategory(start=${page.pages})}">[末页]</a>
    </div>
    <br/>
    <form action="addCategory" method="post">
        name:<input type="text" name="name"/><br/>
        <button type="submit">提交</button>
    </form>
</div>

</body>
</html>

5.测试地址:http://localhost:8080/thymeleaf/listCategory

在这里插入图片描述

6.总结要点

要点1:在PageHelperConfig中我们配置了这一句: p.setProperty(“reasonable”,“true”);//reasonable:启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页。
与是在Controller里面测试,将start的默认值设置为0,访问的地址是http://localhost:8080/thymeleaf/listCategory,此时的start的值是默认的0,由于PageHelperConfig的存在,pageNum将变成1
在这里插入图片描述
在这里插入图片描述
使用PageHelper.startPage 静态方法调用startPage :
特点:

  1. 静态方法,传递两个参数(当前页码,每页查询条数)
  2. 使用pageHelper 分页的时候,不再关注分页语句,查询全部的语句
  3. 自动的对PageHelper.startPage 方法下的第一个sql 查询进行分页
    PageHelper.startPage(1,5);
    //紧跟着的第一个select 方法会被分页
    List list = countryMapper.findAll();

也就是说再Service层PageHelper.startPage(1,5);语句后一定是紧跟查询语句。

 PageHelper.startPage(start, size, "id desc");
        List<Category> categoryList = categoryMapper.findAll();
        PageInfo<Category> page = new PageInfo<>(categoryList);

返回的信息就是pageInfo对象,该类是插件里的类,这个类里面的属性还是值得看一看

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;
传智播客——专注于Java、.Net 和Php、网页平面设计工程师的培训
北京市昌平区建材城西路金燕龙办公楼一层电话:400-618-9090
//所有导航页号
private int[] navigatepageNums;
//导航条上的第一页
private int navigateFirstPage;
//导航条上的最后一页
private int navigateLastPage;
}

从这个属性里在前端取出结果集:

 <tr th:each="c:${page.list}">
            <td th:text="${c.id}"></td>
            <td th:text="${c.name}"></td>
            <td><a th:href="@{editCategory(id=${c.id})}">编辑</a></td>
            <td><a th:href="@{deleteCategory(id=${c.id})}">删除</a></td>
        </tr>

SSM项目

上面的步骤是在springboot项目中配置的,但那个PagehelperConfig我在SSM项目中配置了没有用,于是在SSM项目在spring-mybaits的配置文件中的sqlSession中对其属性进行配置:

 <!--Mybatis的SessionFactory配置-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.how2java.tmall.pojo" />
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
       <!-- 分页插件,目前先注释,后面重构的时候才会使用-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            reasonable=true
                            offsetAsPageNum=true
                            rowBoundsWithCount=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值