SpringBoot整合PageHelper

1、在pom.xml中导入PageHelper分页插件

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

2、配置分页插件得配置文件

pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

3、编写代码

3.1、COntroller层

@Controller
@RequestMapping("/user")
public class UserController {

   @Autowired
   private UserService userService;

   @GetMapping("/findAll")
   public String findAll(Model model,
                         @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                         @RequestParam(value = "limitNum",required = false,defaultValue = "2") Integer limitNum){
       /*PageHelper.startPage(pageNum,limitNum);
       List<User> list = userService.findAllByPage();
       PageInfo<User> pageInfo=new PageInfo<User>(list);*/
       PageInfo<User> pageInfo=userService.findByPage(pageNum,limitNum);
       model.addAttribute("pageInfo",pageInfo);
       return "index";
   }

}

3.2、Service层

@Override
    public PageInfo<User> findByPage(Integer pageNum,Integer limitNum){
        PageHelper.startPage(pageNum,limitNum);
        PageInfo<User> info=new PageInfo<User>(userMapper.findAllByPage());
        return info;
    }

3.3、mapper层

public interface UserMapper {

  public List<User> findAllByPage();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lcb.pagehelper.mapper.UserMapper">
    <select id="findAllByPage" resultType="user">
        select
            id,
            name,
            gender,
            age,
            address,
            qq,
            email
        from
            user
        where
            1=1
    </select>
</mapper>

3.4、页面展示

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>thymeleaf</title>
</head>
<body>
    <div>
        <table>
            <tr>
                <th>id</th>
                <th>name</th>
                <th>gender</th>
                <th>age</th>
                <th>address</th>
                <th>qq</th>
                <th>email</th>
            </tr>
            <tr th:each="u:${pageInfo.list}">
                <td th:text="${u.id}">id</td>
                <td th:text="${u.name}">name</td>
                <td th:text="${u.gender}">gender</td>
                <td th:text="${u.age}">age</td>
                <td th:text="${u.address}">address</td>
                <td th:text="${u.qq}">qq</td>
                <td th:text="${u.email}">email</td>
            </tr>
        </table>
        <p>当前 <span th:text="${pageInfo.pageNum}"></span> 页,总 <span th:text="${pageInfo.pages}"></span> 页,共 <span th:text="${pageInfo.total}"></span> 条记录</p>
        <a th:href="@{/user/findAll}">首页</a>
        <a th:href="@{/user/findAll(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a>
        <a th:href="@{/user/findAll(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a>
        <a th:href="@{/user/findAll(pageNum=${pageInfo.pages})}">尾页</a>
    </div>

</body>
</html>

4、启动测试

在这里插入图片描述

5、关于pageHelper

PageHelper.startPage(int PageNum,int PageSize):用来设置页面的位置和展示的数据条目数
PageInfo.list:结果集
PageInfo.pageNum :当前页码
PageInfo.pageSize :当前页面显示的数据条目
PageInfo.pages: 总页数
PageInfo.total: 数据的总条目数
PageInfo.prePage: 上一页
PageInfo.nextPage :下一页
PageInfo.isFirstPage: 是否为第一页
PageInfo.isLastPage: 是否为最后一页
PageInfo.hasPreviousPage: 是否有上一页
PageHelper.hasNextPage: 是否有下一页

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值