springboot+thymeleaf+pagehelper实现分页

1.引入依赖

注意:pagehelper使用的是整合springboot的依赖

 <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>

2.yml设置

# 分页配置
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

3.后台代码

controller

 @RequestMapping("/list")
    public String getList(@RequestParam(value = "pageNo",defaultValue = "1") int pageNo, Map<String,Object> map){
    
    
        PageInfo<User> page = userService.getLists(pageNo);
        map.put("page",page);
        return "BackGround/book";  //转发到你自己的页面
    }

service

public PageInfo<User> getLists(int pageNo);

impl

    @Override
    public PageInfo<User> getLists(int pageNo) {
    
    
        PageHelper.startPage(pageNo, ConstantUtils.PAGE_SIZE);//ConstantUtils.PAGE_SIZE 是设置每页显示条数
        List<User> list  = userMapper.getList();//获取数据集合
        PageInfo<User> pageInfo = new PageInfo<>(list, ConstantUtils.NAVIGATE_PAGENUM);//ConstantUtils.NAVIGATE_PAGENUM 是分页中间显示的那个玩意
        return pageInfo;
    }

4.页面

没有加任何样式,相加样式自己去百度啊~

    <div th:if="${page.pages>0}">
            <!--当前页-->
            <span th:text="'当前页:'+${page.pageNum}"></span>
            &nbsp;&nbsp;
            <!--            首页作为第一页不可点击-->
            <a th:href="@{/list(pageNo=1)}">首页</a>

            <span th:if="${page.pageNum==1}">
                <a onclick="return false">上一页</a>
            </span>
            <span th:unless="${page.pageNum==1}">
                <a th:href="@{/list(pageNo=${page.pageNum-1})}">上一页</a>
            </span>

            <span th:each="i:${#numbers.sequence(1,page.pages)}">
                <a th:href="@{/list(pageNo=${i})}" th:text="${i}">1</a>
            </span>

            <span th:if="${page.pages==page.pageNum}">
                <a onclick="return false">下一页</a>
            </span>

            <span th:unless="${page.pages==page.pageNum}">
                <a th:href="@{/list(pageNo=${page.pageNum+1})}">下一页</a>
            </span>

            <a th:href="@{/list(pageNo=${page.pages})}">末页</a>

            <span th:text="'总条数'+${page.total}"></span>
        </div>

Guess you like

Origin blog.csdn.net/JavaSupeMan/article/details/115473641