Springboot + Jpa +thymeleaf 使用 pageable实现分页

本文参考 springboot使用JPA分页pageable分页;mybatis用pagehelper分页。分享两种分页方法

SpringBoot + thymeleaf 实现分页

在这里插入图片描述

说说自己踩过的坑吧

  • pageHelper 在看到参考文前,我使用的是pageHelper,看了不下5篇文章,也上过github查看pageHelper的官方页面,不论是
    ------在启动项添加注解
    ------在application.properties添加的说明
    ------修改springboot的版本(pageHelper截至目前支持springboot 2.3.1版本)
    ------pageHelper.starPage()后一句严格要求为查询语句

    都无法在我的网页上实现分页,都是显示我数据库中的所有条目

但在今天下午终于实现了分页功能,哭了,下面分享出我分页相关代码
先看一下项目的具体结构
在这里插入图片描述

  • Controller 分页设置
		//...查找数据库  将结果返回给books(List<Book>) eg: books.bookService.getBookList();
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        // PageUtil.listConvertToPage 自定义的 List<> --> Page<> 的方法
        Page<Book> list = PageUtil.listConvertToPage(books, pageable);
        //list送给前端
        model.addAttribute("pageInfo", list);
  • PageUtil 类
public class PageUtil {
    public static  <T> Page<T> listConvertToPage(List<T> list, Pageable pageable) {
        int start = (int) pageable.getOffset();
        int end = (start + pageable.getPageSize()) > list.size() ? list.size() : (start + pageable.getPageSize());
        return new PageImpl<T>(list.subList(start, end), pageable, list.size());
    }
}
  • list.html
    表单部分
<table class="table table-hover">

            <tr>
                <th>图书id</th>
                <th>图书名</th>
                <th>作者</th>
                <th>出版社</th>
                <th>ISBN</th>
                <th>类别</th>
                <th>价格</th>
                <th>操作</th>
                <th>操作</th>
            </tr>
            <tr th:each="book : ${pageInfo.getContent()}">
                <th scope="row" th:text="${book.id}">1</th>
                <td th:text="${book.title}">neo</td>
                <td th:text="${book.author}">123456</td>
                <td th:text="${book.publish}">123456</td>
                <td th:text="${book.isbn}">0</td>
                <td th:text="${book.category.name}">0</td>
                <td th:text="${book.price}">0</td>
                <td><a th:href="@{edit/{id}(id=${book.id})}">编辑</a></td>
                <td><a th:href="@{delete/{id}(id=${book.id})}">删除</a></td>
            </tr>
        </table>

分页部分(居中让我查了好久,前端一点都不懂ei)

<!--分页-->
<div class="modal-footer ">

    <div class="col-md-6 col-xs-8 col-xs-offset-1">
        <ul class="pagination ">

            <!-- 首页 -->
            <li>
                <a th:href="'/book/list?pageNum=1'+'&msg='+${searchInfo.get('msg')}+'&type='+${searchInfo.get('type')}"
                   }>首页</a>
            </li>

            <!-- 上一页 -->
            <li th:if="${pageInfo.hasPrevious()}">
                <a th:href="'/book/list?pageNum=' + ${pageInfo.previousPageable().getPageNumber()+1}+'&msg='+${searchInfo.get('msg')}+'&type='+${searchInfo.get('type')}"
                   th:text="上一页"></a>
            </li>

            <!-- 中间页 -->
            <li th:each="pageNum:${#numbers.sequence(0, pageInfo.getTotalPages()-1)}">
                <a th:href="'/book/list?pageNum=' + ${pageNum+1}+'&msg='+${searchInfo.get('msg')}+'&type='+${searchInfo.get('type')}"
                   th:text="${pageNum + 1}"
                   th:if="${pageNum ne pageInfo.pageable.getPageNumber()}"></a>
                <a th:href="'/book/list?pageNum=' + ${pageNum+1}+'&msg='+${searchInfo.get('msg')}+'&type='+${searchInfo.get('type')}"
                   th:text="${pageNum + 1}"
                   th:if="${pageNum eq pageInfo.pageable.getPageNumber()}"
                   th:style="'font-weight:bold;background: #6faed9;'"></a>
            </li>

            <!-- 下一页 -->
            <li th:if="${pageInfo.hasNext()}">
                <a th:href="'/book/list?pageNum=' + ${pageInfo.nextPageable().getPageNumber()+1}+'&msg='+${searchInfo.get('msg')}+'&type='+${searchInfo.get('type')}"
                   th:text="下一页"></a>
            </li>

            <!-- 尾页 -->
            <li>
                <a th:href="'/book/list?pageNum=' + ${pageInfo.getTotalPages()}+'&msg='+${searchInfo.get('msg')}+'&type='+${searchInfo.get('type')}">尾页</a>
            </li>

        </ul>
    </div>

</div>

具体项目:github

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是示例代码。 首先是 Entity 类: ```java @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long user_id; private String user_name; private String account; private String password; private String phone; private String address; private String role; // 省略 getter 和 setter 方法 } ``` 然后是 DAO 层: ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { } ``` 接下来是 Service 层: ```java @Service public class UserService { @Autowired private UserRepository userRepository; public List<User> getAllUsers() { return userRepository.findAll(); } public Page<User> getUsersByPage(int pageNum, int pageSize) { Pageable pageable = PageRequest.of(pageNum - 1, pageSize); return userRepository.findAll(pageable); } } ``` 最后是 Controller 层: ```java @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/user/check") public String checkUsers(Model model, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) { Page<User> users = userService.getUsersByPage(pageNum, pageSize); model.addAttribute("users", users.getContent()); model.addAttribute("currentPage", users.getNumber() + 1); model.addAttribute("totalPages", users.getTotalPages()); return "user-check"; } } ``` 在 Thymeleaf 模板文件 user-check.html 中,可以使用类似如下的代码来显示查询到的用户列表: ```html <table> <thead> <tr> <th>user_id</th> <th>user_name</th> <th>account</th> <th>password</th> <th>phone</th> <th>address</th> <th>role</th> </tr> </thead> <tbody> <tr th:each="user : ${users}"> <td th:text="${user.user_id}"></td> <td th:text="${user.user_name}"></td> <td th:text="${user.account}"></td> <td th:text="${user.password}"></td> <td th:text="${user.phone}"></td> <td th:text="${user.address}"></td> <td th:text="${user.role}"></td> </tr> </tbody> </table> <div> <ul class="pagination"> <li th:class="${currentPage == 1} ? disabled : ''"> <a th:href="@{/user/check(pageNum=1,pageSize=${pageSize})}" aria-label="First"> <span aria-hidden="true">«</span> </a> </li> <li th:class="${currentPage == 1} ? disabled : ''"> <a th:href="@{/user/check(pageNum=${currentPage - 1},pageSize=${pageSize})}" aria-label="Previous"> <span aria-hidden="true"><</span> </a> </li> <li th:class="${currentPage == totalPages} ? disabled : ''"> <a th:href="@{/user/check(pageNum=${currentPage + 1},pageSize=${pageSize})}" aria-label="Next"> <span aria-hidden="true">></span> </a> </li> <li th:class="${currentPage == totalPages} ? disabled : ''"> <a th:href="@{/user/check(pageNum=${totalPages},pageSize=${pageSize})}" aria-label="Last"> <span aria-hidden="true">»</span> </a> </li> </ul> </div> ``` 以上就是使用 Spring Boot、Hibernate 和 Thymeleaf 实现分页查询所有用户的示例代码了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值