Spring Data JPA - Web 支持、排序和分页

news2024/9/23 5:22:53

在前面的教程中,我们已经看到了如何使用 Spring Data 分页 Web 支持。在本教程中,我们将了解如何使用排序支持。

在基于 Web 的应用程序中应用排序主要有两种方法:

按可分页处理程序参数排序

我们在前面的示例中使用过,但没有任何排序查询参数。除了分页信息外,还可以设置排序信息(查看这个和这个)。在这种情况下,我们必须使用类似于以下内容的查询字符串:PageablePageable

?page=1&size=10&sort=personName,desc

相当于:

  PageRequest.of(1,10, Sort.by("personName").descending())

按排序处理程序参数排序

我们也可以用作控制器方法参数(查看我们的排序基本示例)。在这种情况下,我们需要创建类似于以下内容的查询字符串:Sort

?sort=personName,desc

相当于:

  Sort.by("personName").descending()

@SortDefault注释

此批注定义将 Sort 实例注入控制器处理程序方法时要使用的默认排序选项。

SortHandlerMethodArgumentResolver

在上述两种情况下,用于从请求参数或 SortDefault 注释自动创建排序实例。当我们在配置类中使用时,此解析器处于活动状态。SortHandlerMethodArgumentResolver@EnableSpringDataWebSupport

实体

@Entity
public class Employee {
  private @Id
  @GeneratedValue
  Long id;
  private String name;
  private String dept;
  private int salary;
    .............
}

存储 库

public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Long> {

  @Query("SELECT DISTINCT e.dept FROM Employee e")
  List<String> findAllDepartments(Sort sort);
}

我们在上面的存储库 withparameter 中创建了一个自定义查询方法,以便我们可以在控制器中使用 Sort 参数,该参数只会为视图生成部门列表。Sort

MVC 控制器

在下面的控制器中,我们将使用两种处理程序方法,一种是使用可分页参数,另一种是使用 Sort 参数:

@Controller
public class EmployeeController {

  @Autowired
  private EmployeeRepository repository;

  @GetMapping("/employees")
  public String getEmployees(@PageableDefault(size = 10, sort = "id") Pageable pageable,
                             Model model) {
      Page<Employee> page = repository.findAll(pageable);
      List<Sort.Order> sortOrders = page.getSort().stream().collect(Collectors.toList());
      if (sortOrders.size() > 0) {
          Sort.Order order = sortOrders.get(0);
          model.addAttribute("sortProperty", order.getProperty());
          model.addAttribute("sortDesc", order.getDirection() == Sort.Direction.DESC);
      }
      model.addAttribute("page", page);
      return "employee-page";
  }

  @GetMapping("departments")
  public String getDepartments(@SortDefault(sort="dept",direction = Sort.Direction.ASC)
                                           Sort sort, Model model) {
      List<String> depts = repository.findAllDepartments(sort);
      model.addAttribute("depts", depts);
      return "dept-page";
  }
}

Thymeleaf 视图

跟随视图同时实现排序和分页。我们正在通过单击表标题进行单列排序。这类似于Java Swing JTable排序功能(在此处查看示例)。

我们使用 JQuery 来处理表头单击和显示箭头;▾ 和 ▴ 分别在正确的位置进行降序和升序排序。

src/main/webapp/WEB-INF/views/employee-page.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script th:inline="javascript">
    //thymeleaf to javascript variables
    /*<![CDATA[*/
    var sortProperty = /*[[${sortProperty}]]*/ null;
    var sortDesc = /*[[${sortDesc}]]*/ null;
    var currentPage = /*[[${page.number}]]*/ 0;
    var pageSize =/*[[${page.size}]]*/ 0;
    /*]]>*/

    $(document).ready(function(){
     //show up/down arrows
     $("table#emp-table thead th").each(function(){
        var head = $(this);
        if(head.attr('data-sort-prop')==sortProperty){
            head.append(sortDesc?'▾':'▴');
        }
        });

     //set click action, reload page on clicking with all query params
     $("table#emp-table thead th").click(function() {
      var headerSortPropName = $(this).attr("data-sort-prop");
      if(headerSortPropName==sortProperty){
          window.location.href = window.location.pathname+
          '?page='+currentPage+'&size='+pageSize+'&sort='+ headerSortPropName+','+
          (sortDesc?'asc':'desc');
      }else{
           window.location.href = window.location.pathname+
          '?page='+currentPage+'&size='+pageSize+'&sort='+ headerSortPropName+',asc';
      }
      });
    });
  </script>
  <style>
     table{width:100%;}
     table td, table th { border: 1px solid grey;}
     table th { user-select: none; background: #eee;}
     table tr th:first-child{width:100px;}
     table tr th:nth-child(3){width:150px;}
     table tr th:nth-child(4){width:150px;}
     .pagination-div{user-select: none;}
     .pagination-div span{border-radius:3px;border:1px solid #999;
         padding:5px;margin:10px 0px 0px 10px;display:inline-block}
     span.selected{background:#ccf;}
  </style>
</head>
<body>
<h2>Employees</h2>
<table id="emp-table">
    <thead>
    <tr>
        <th data-sort-prop="id">Id</th>
        <th data-sort-prop="name">Name</th>
        <th data-sort-prop="dept">Department</th>
        <th data-sort-prop="salary">Salary</th>
    </tr>
    </thead>
    <tr th:each="employee : ${page.content}">
        <td th:text="${employee.id}"></td>
        <td th:text="${employee.name}"></td>
        <td th:text="${employee.dept}"></td>
        <td th:text="${employee.salary}"></td>
    </tr>
</table>
<!-- using th:with to declare a local variable for sorting query param -->
<div class="pagination-div" th:with="sortParam=${sortProperty+','+(sortDesc?'desc':'asc')}">
    <span th:if="${page.hasPrevious()}">
        <a th:href="@{/employees(page=${page.number-1},size=${page.size},sort=${sortParam})}">Previous</a>
    </span>
    <th:block th:each="i: ${#numbers.sequence(0, page.totalPages - 1)}">
        <span th:if="${page.number == i}" class="selected">[[${i}+1]]</span>
        <span th:unless="${page.number == i}">
             <a th:href="@{/employees(page=${i},size=${page.size},sort=${sortParam})}">[[${i}+1]]</a>
        </span>
    </th:block>
    <span th:if="${page.hasNext()}">
        <a th:href="@{/employees(page=${page.number+1},size=${page.size},sort=${sortParam})}">Next</a>
    </span>
</div>
</body>
</html>

以下视图仅显示部门列表。相应的处理程序方法(如上所示)仅使用参数。Sort

src/main/webapp/WEB-INF/views/dept-page.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<body>
<h2>Departments</h2>
    <p th:each="dept : ${depts}">
        <td th:text="${dept}"></td>
    </p>
<br>
<a th:href="@{/departments(sort='dept,asc')}">In ascending order</a> <br/>
<a th:href="@{/departments(sort='dept,desc')}">In descending order</a>
</body>
</html>

运行

要尝试示例,请运行以下示例项目的嵌入式tomcat(在pom中配置.xml):

mvn tomcat7:run-war

输出

localhost:8080/employees

如上所示,默认情况下选择第一页,并按升序对“id”列进行排序。让我们点击“名称”列标题:

查看上面地址栏中的查询字符串,其中存在所有必需的查询参数。单击“名称”标题上的更多时间将更改排序方向:

单击分页按钮可保留上次排序的列。

让我们通过本地主机访问部门页面localhost:8080/departments

单击底部的“降序”链接将按降序显示部门:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
    <head>
        <link rel="icon" th:href="@{/assets/img/favicon.png}"  />
        <link rel="stylesheet" type="text/css" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}" />
        <link rel="stylesheet" type="text/css" th:href="@{/webjars/font-awesome/css/all.min.css}"/>
        <link rel="stylesheet" type="text/css" th:href="@{/webjars/bootstrap-icons/font/bootstrap-icons.css}"/>
        <link rel="stylesheet" type="text/css" th:href="@{/assets/css/style.css}" />
        <link rel="stylesheet" type="text/css" th:href="@{/css/style.css}" />
        <script type="text/javascript" th:src="@{/webjars/jquery/jquery.min.js}"></script>
        <script type="text/javascript" th:src="@{/webjars/bootstrap/js/bootstrap.bundle.min.js}"></script>
        <script th:inline="javascript">
            //thymeleaf to javascript variables
            /*<![CDATA[*/
            var sortProperty = /*[[${sortProperty}]]*/ null;
            var sortDesc = /*[[${sortDesc}]]*/ null;
            var currentPage = /*[[${page.number}]]*/ 0;
            var pageSize = /*[[${page.size}]]*/ 0;
            /*]]>*/

            $(document).ready(function () {
                //show up/down arrows
                $("table#emp-table thead th").each(function () {
                    var head = $(this);
                    if (head.attr('data-sort-prop') == sortProperty) {
                        head.append(sortDesc ? '&#9662;' : '&#9652;');
                    }
                });

                //set click action, reload page on clicking with all query params
                $("table#emp-table thead th").click(function () {
                    var headerSortPropName = $(this).attr("data-sort-prop");
                    if (headerSortPropName == sortProperty) {
                        window.location.href = window.location.pathname +
                                '?page=' + currentPage + '&size=' + pageSize + '&sort=' + headerSortPropName + ',' +
                                (sortDesc ? 'asc' : 'desc');
                    } else {
                        window.location.href = window.location.pathname +
                                '?page=' + currentPage + '&size=' + pageSize + '&sort=' + headerSortPropName + ',asc';
                    }
                });
            });
        </script>
        <style>
            table{
                width:100%;
            }
            table td, table th {
                border: 1px solid grey;
            }
            table th {
                user-select: none;
                background: #eee;
            }
            table tr th:first-child{
                width:100px;
            }
            table tr th:nth-child(3){
                width:150px;
            }
            table tr th:nth-child(4){
                width:150px;
            }
            .pagination-div{
                user-select: none;
            }
            .pagination-div span{
                border-radius:3px;
                border:1px solid #999;
                padding:5px;
                margin:10px 0px 0px 10px;
                display:inline-block
            }
            span.selected{
                background:#ccf;
            }
        </style>
    </head>
    <body>
        <h2>Employees</h2>
        <div class="my-3 ">

            <div class="row d-flex flex-row">
                <form th:action="@{/employees}" id="searchForm" >
                    <div class="row">
                        <div class="col-sm-8">
                            <div class="col-md-12 mt-2">
                                <div class="search">
                                    <i class="fa fa-search"></i>
                                    <input id="keyword" type="search" name="keyword" th:value="${keyword}" required class="form-control"
                                           placeholder="Enter keyword">
                                        <button type="submit" class="btn btn-secondary">搜索</button>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <div class="col-md-12 input-group mt-2">
                                <div class="input-group-prepend">
                                    <label class="input-group-text" for="pageSize">每页显示行数:</label>
                                </div>
                                <select form="searchForm" name="size" th:value="${pageSize}" onchange="changePageSize()" class="size-select"
                                        id="pageSize">
                                    <option th:each="s : ${ {10, 25, 50} }" th:value="${s}" th:text="${s}" th:selected="${s == page.size}"></option>
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-1">
                            <div class="col-md-12 mt-2">
                                <button id="btnClear" class="btn btn-info">重置</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>

        </div>
        <table id="emp-table">
            <thead>
                <tr>
                    <th data-sort-prop="id">Id</th>
                    <th data-sort-prop="name">Name</th>
                    <th data-sort-prop="dept">Department</th>
                    <th data-sort-prop="salary">Salary</th>
                </tr>
            </thead>
            <tr th:each="employee : ${page.content}">
                <td th:text="${employee.id}"></td>
                <td th:text="${employee.name}"></td>
                <td th:text="${employee.dept}"></td>
                <td th:text="${employee.salary}"></td>
            </tr>
        </table>
        <!-- using th:with to declare a local variable for sorting query param -->
        <div class="pagination-div" th:with="sortParam=${sortProperty+','+(sortDesc?'desc':'asc')}">
            <span th:if="${page.hasPrevious()}">
                <a th:href="@{/employees(page=${page.number-1},size=${page.size},sort=${sortParam})}">Previous</a>
            </span>
            <th:block th:each="i: ${#numbers.sequence(0, page.totalPages - 1)}">
                <span th:if="${page.number == i}" class="selected">[[${i}+1]]</span>
                <span th:unless="${page.number == i}">
                    <a th:href="@{/employees(page=${i},size=${page.size},sort=${sortParam})}">[[${i}+1]]</a>
                </span>
            </th:block>
            <span th:if="${page.hasNext()}">
                <a th:href="@{/employees(page=${page.number+1},size=${page.size},sort=${sortParam})}">Next</a>
            </span>
        </div>

        <div class="row">
            <div class="col-sm-6">
                <div th:if="${page.totalPages > 0}">
                    总计<span th:text="${page.totalElements}">99</span>行,当前显示<span th:text="(${page.number} )*${page.size} +1">1</span> -
                    <span th:text="(${page.number} )*${page.size} +${page.numberOfElements}">5</span>行&nbsp; &nbsp;总计<span th:text="${page.totalPages}">99</span>页
                </div>
            </div>
            <div class="col-sm-6" th:with="sortParam=${sortProperty+','+(sortDesc?'desc':'asc')}">
                <nav aria-label="Pagination" th:if="${page.totalPages > 0}">
                    <ul class="pagination justify-content-center">
                        <li class="page-item" th:classappend="${page.number == 0} ? 'disabled'">
                            <a class="page-link"
                               th:href="@{'/employees?' + ${keyword!=null && keyword!=''? 'keyword=' + keyword + '&' : ''} + 'page=' +'0'+ '&size=' + ${page.size}+'&sort='+${sortParam}}"
                               title="First Page" rel="First Page">
                                最前页
                            </a>
                        </li>
                        <li class="page-item font-weight-bold" th:classappend="${page.number == 0} ? 'disabled'">
                            <a class="page-link"
                               th:href="@{'/employees?' + ${keyword!=null && keyword!=''? 'keyword=' + keyword + '&' : ''} + 'page=' + ${page.number - 1} + '&size=' + ${page.size}+'&sort='+${sortParam}}"
                               title="Previous Page" rel="Previous Page">
                                前一页
                            </a>
                        </li>
                        <li class="page-item disabled" th:if="${page.number - 2 > 0}">
                            <a class="page-link" href="#">...</a>
                        </li>
                        <li
                            th:each="i : ${#numbers.sequence(page.number > 2 ? page.number - 2 : 0, page.number + 2 < page.totalPages ? page.number + 2 : page.totalPages-1)}"
                            class="page-item" th:classappend="${i == page.number} ? 'active'">
                            <a class="page-link"
                               th:href="@{'/employees?' + ${keyword!=null && keyword!=''? 'keyword=' + keyword + '&' : ''} + 'page=' + ${i} + '&size=' + ${page.size}+'&sort='+${sortParam}}"
                               th:title=${i+1} th:rel=${i+1}>
                                [[${i+1} ]]
                            </a>
                        </li>
                        <li class="page-item disabled" th:if="${page.number + 2 < page.totalPages}">
                            <a class="page-link" href="#">...</a>
                        </li>
                        <li class="page-item font-weight-bold" th:classappend="${page.number == page.totalPages-1} ? 'disabled'">
                            <a class="page-link"
                               th:href="@{'/employees?' + ${keyword!=null && keyword!=''? 'keyword=' + keyword + '&' : ''} + 'page=' + ${page.number + 1} + '&size=' + ${page.size}+'&sort='+${sortParam}}"
                               title="Next Page" rel="Next Page">
                                后一页
                            </a>
                        </li>
                        <li class="page-item" th:classappend="${page.number == page.totalPages-1} ? 'disabled'">
                            <a class="page-link"
                               th:href="@{'/employees?' + ${keyword!=null && keyword!=''? 'keyword=' + keyword + '&' : ''} + 'page=' +${page.totalPages-1}+ '&size=' + ${page.size}+'&sort='+${sortParam}}"
                               title="Last Page" rel="Last Page">
                                最后页
                            </a>
                        </li>
                    </ul>
                </nav>
            </div>
        </div>
        <a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i
                class="bi bi-arrow-up-short"></i></a>
        <script type="text/javascript" th:src="@{/webjars/jquery/jquery.min.js}"></script>
        <script type="text/javascript" th:src="@{/webjars/bootstrap/js/bootstrap.bundle.min.js}"></script>
        <script type="text/javascript" th:src="@{/assets/js/main.js}" ></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $(".btn-delete").on("click", function (e) {
                    e.preventDefault();
                    link = $(this);

                    tutorialTitle = link.attr("tutorialTitle");
                    $("#yesBtn").attr("href", link.attr("href"));
                    $("#confirmText").html("Do you want to delete the Tutorial \<strong\>" + tutorialTitle + "\<\/strong\>?");
                    var myModal = new bootstrap.Modal(document.getElementById('confirmModal'), {
                        keyboard: false
                    });
                    myModal.toggle();
                });

                $("#btnClear").on("click", function (e) {
                    e.preventDefault();
                    $("#keyword").text("");
                    window.location = "[[@{/employees}]]";
                });
            });

            function changePageSize() {
                $("#searchForm").submit();
            }
        </script>
    </body>
</html>

添加搜索/过滤功能

package com.example;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface EmployeeRepository extends JpaRepository<Employee, Long> {

    @Query("SELECT e FROM Employee e WHERE CONCAT(e.name, ' ', e.dept) LIKE %?1%")
    public Page<Employee> search(String keyword, Pageable pageable);
}
package com.example;

import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.Param;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class EmployeeController {

    @Autowired
    private EmployeeRepository repository;

    @GetMapping("/employees")
    public String getEmployees(@Param("keyword") String keyword, @PageableDefault(size = 10, sort = "id") Pageable pageable,
            Model model) {

        if (keyword == null) {
            Page<Employee> page = repository.findAll(pageable);
            List<Sort.Order> sortOrders = page.getSort().stream().collect(Collectors.toList());
            if (sortOrders.size() > 0) {
                Sort.Order order = sortOrders.get(0);
                model.addAttribute("sortProperty", order.getProperty());
                model.addAttribute("sortDesc", order.getDirection() == Sort.Direction.DESC);
            }
            model.addAttribute("page", page);
        } else {
            Page<Employee> page = repository.search(keyword, pageable);
            List<Sort.Order> sortOrders = page.getSort().stream().collect(Collectors.toList());
            if (sortOrders.size() > 0) {
                Sort.Order order = sortOrders.get(0);
                model.addAttribute("sortProperty", order.getProperty());
                model.addAttribute("sortDesc", order.getDirection() == Sort.Direction.DESC);
            }
            model.addAttribute("keyword", keyword);
            model.addAttribute("page", page);
        }

        return "employee-page";

    }

}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
    <head>
        <link rel="icon" th:href="@{/assets/img/favicon.png}"  />
        <link rel="stylesheet" type="text/css" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}" />
        <link rel="stylesheet" type="text/css" th:href="@{/webjars/font-awesome/css/all.min.css}"/>
        <link rel="stylesheet" type="text/css" th:href="@{/webjars/bootstrap-icons/font/bootstrap-icons.css}"/>
        <link rel="stylesheet" type="text/css" th:href="@{/assets/css/style.css}" />
        <link rel="stylesheet" type="text/css" th:href="@{/css/style.css}" />
        <script type="text/javascript" th:src="@{/webjars/jquery/jquery.min.js}"></script>
        <script type="text/javascript" th:src="@{/webjars/bootstrap/js/bootstrap.bundle.min.js}"></script>
        <script th:inline="javascript">
            //thymeleaf to javascript variables
            /*<![CDATA[*/
            var sortProperty = /*[[${sortProperty}]]*/ null;
            var sortDesc = /*[[${sortDesc}]]*/ null;
            var currentPage = /*[[${page.number}]]*/ 0;
            var pageSize = /*[[${page.size}]]*/ 0;
            /*]]>*/

            $(document).ready(function () {
                //show up/down arrows
                $("table#emp-table thead th").each(function () {
                    var head = $(this);
                    if (head.attr('data-sort-prop') == sortProperty) {
                        head.append(sortDesc ? '&#9662;' : '&#9652;');
                    }
                });

                //set click action, reload page on clicking with all query params
                $("table#emp-table thead th").click(function () {
                    var headerSortPropName = $(this).attr("data-sort-prop");
                    if (headerSortPropName == sortProperty) {
                        window.location.href = window.location.pathname +
                                '?page=' + currentPage + '&size=' + pageSize + '&sort=' + headerSortPropName + ',' +
                                (sortDesc ? 'asc' : 'desc');
                    } else {
                        window.location.href = window.location.pathname +
                                '?page=' + currentPage + '&size=' + pageSize + '&sort=' + headerSortPropName + ',asc';
                    }
                });
            });
        </script>
        <style>
            table{
                width:100%;
            }
            table td, table th {
                border: 1px solid grey;
            }
            table th {
                user-select: none;
                background: #eee;
            }
            table tr th:first-child{
                width:100px;
            }
            table tr th:nth-child(3){
                width:150px;
            }
            table tr th:nth-child(4){
                width:150px;
            }
            .pagination-div{
                user-select: none;
            }
            .pagination-div span{
                border-radius:3px;
                border:1px solid #999;
                padding:5px;
                margin:10px 0px 0px 10px;
                display:inline-block
            }
            span.selected{
                background:#ccf;
            }
        </style>
    </head>
    <body>
        <h2>Employees</h2>
        <div class="my-3 ">

            <div class="row d-flex flex-row">
                <form th:action="@{/employees}" id="searchForm" >
                    <div class="row">
                        <div class="col-sm-8">
                            <div class="col-md-12 mt-2">
                                <div class="search">
                                    <i class="fa fa-search"></i>
                                    <input id="keyword" type="search" name="keyword" th:value="${keyword}" required class="form-control"
                                           placeholder="Enter keyword">
                                        <button type="submit" class="btn btn-secondary">搜索</button>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <div class="col-md-12 input-group mt-2">
                                <div class="input-group-prepend">
                                    <label class="input-group-text" for="pageSize">每页显示行数:</label>
                                </div>
                                <select form="searchForm" name="size" th:value="${pageSize}" onchange="changePageSize()" class="size-select"
                                        id="pageSize">
                                    <option th:each="s : ${ {10, 25, 50} }" th:value="${s}" th:text="${s}" th:selected="${s == page.size}"></option>
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-1">
                            <div class="col-md-12 mt-2">
                                <button id="btnClear" class="btn btn-info">重置</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>

        </div>
        <table id="emp-table">
            <thead>
                <tr>
                    <th data-sort-prop="id">Id</th>
                    <th data-sort-prop="name">Name</th>
                    <th data-sort-prop="dept">Department</th>
                    <th data-sort-prop="salary">Salary</th>
                </tr>
            </thead>
            <tr th:each="employee : ${page.content}">
                <td th:text="${employee.id}"></td>
                <td th:text="${employee.name}"></td>
                <td th:text="${employee.dept}"></td>
                <td th:text="${employee.salary}"></td>
            </tr>
        </table>


        <div class="row">
            <div class="col-sm-6">
                <div th:if="${page.totalPages > 0}">
                    总计<span th:text="${page.totalElements}">99</span>行,当前显示<span th:text="(${page.number} )*${page.size} +1">1</span> -
                    <span th:text="(${page.number} )*${page.size} +${page.numberOfElements}">5</span>行&nbsp; &nbsp;总计<span th:text="${page.totalPages}">99</span>页
                </div>
            </div>
            <div class="col-sm-6" th:with="sortParam=${sortProperty+','+(sortDesc?'desc':'asc')}">
                <nav aria-label="Pagination" th:if="${page.totalPages > 0}">
                    <ul class="pagination justify-content-center">
                        <li class="page-item" th:classappend="${page.number == 0} ? 'disabled'">
                            <a class="page-link"
                               th:href="@{'/employees?' + ${keyword!=null && keyword!=''? 'keyword=' + keyword + '&' : ''} + 'page=' +'0'+ '&size=' + ${page.size}+'&sort='+${sortParam}}"
                               title="First Page" rel="First Page">
                                最前页
                            </a>
                        </li>
                        <li class="page-item font-weight-bold" th:classappend="${page.number == 0} ? 'disabled'">
                            <a class="page-link"
                               th:href="@{'/employees?' + ${keyword!=null && keyword!=''? 'keyword=' + keyword + '&' : ''} + 'page=' + ${page.number - 1} + '&size=' + ${page.size}+'&sort='+${sortParam}}"
                               title="Previous Page" rel="Previous Page">
                                前一页
                            </a>
                        </li>
                        <li class="page-item disabled" th:if="${page.number - 2 > 0}">
                            <a class="page-link" href="#">...</a>
                        </li>
                        <li
                            th:each="i : ${#numbers.sequence(page.number > 2 ? page.number - 2 : 0, page.number + 2 < page.totalPages ? page.number + 2 : page.totalPages-1)}"
                            class="page-item" th:classappend="${i == page.number} ? 'active'">
                            <a class="page-link"
                               th:href="@{'/employees?' + ${keyword!=null && keyword!=''? 'keyword=' + keyword + '&' : ''} + 'page=' + ${i} + '&size=' + ${page.size}+'&sort='+${sortParam}}"
                               th:title=${i+1} th:rel=${i+1}>
                                [[${i+1} ]]
                            </a>
                        </li>
                        <li class="page-item disabled" th:if="${page.number + 2 < page.totalPages}">
                            <a class="page-link" href="#">...</a>
                        </li>
                        <li class="page-item font-weight-bold" th:classappend="${page.number == page.totalPages-1} ? 'disabled'">
                            <a class="page-link"
                               th:href="@{'/employees?' + ${keyword!=null && keyword!=''? 'keyword=' + keyword + '&' : ''} + 'page=' + ${page.number + 1} + '&size=' + ${page.size}+'&sort='+${sortParam}}"
                               title="Next Page" rel="Next Page">
                                后一页
                            </a>
                        </li>
                        <li class="page-item" th:classappend="${page.number == page.totalPages-1} ? 'disabled'">
                            <a class="page-link"
                               th:href="@{'/employees?' + ${keyword!=null && keyword!=''? 'keyword=' + keyword + '&' : ''} + 'page=' +${page.totalPages-1}+ '&size=' + ${page.size}+'&sort='+${sortParam}}"
                               title="Last Page" rel="Last Page">
                                最后页
                            </a>
                        </li>
                    </ul>
                </nav>
            </div>
        </div>
        <a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i
                class="bi bi-arrow-up-short"></i></a>
        <script type="text/javascript" th:src="@{/webjars/jquery/jquery.min.js}"></script>
        <script type="text/javascript" th:src="@{/webjars/bootstrap/js/bootstrap.bundle.min.js}"></script>
        <script type="text/javascript" th:src="@{/assets/js/main.js}" ></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $(".btn-delete").on("click", function (e) {
                    e.preventDefault();
                    link = $(this);

                    tutorialTitle = link.attr("tutorialTitle");
                    $("#yesBtn").attr("href", link.attr("href"));
                    $("#confirmText").html("Do you want to delete the Tutorial \<strong\>" + tutorialTitle + "\<\/strong\>?");
                    var myModal = new bootstrap.Modal(document.getElementById('confirmModal'), {
                        keyboard: false
                    });
                    myModal.toggle();
                });

                $("#btnClear").on("click", function (e) {
                    e.preventDefault();
                    $("#keyword").text("");
                    window.location = "[[@{/employees}]]";
                });
            });

            function changePageSize() {
                $("#searchForm").submit();
            }
        </script>
    </body>
</html>

 

示例项目

https://www.logicbig.com/tutorials/spring-framework/spring-data/sorting-and-pagination/spring-data-jpa-sorting-and-pagination.zip

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/57521.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【经验】Word 2021|如何在Word里做出和Markdown中一样漂亮的引用样式(结尾附成品)

文章目录写在最前方法以及参数1 打开样式窗口2 设置一些基本操作3 打开格式窗口4 修改样式最后一步&#xff01;保持间隔成品的介绍&#xff08;一些自卖自夸&#xff09;获取链接&#xff08;不想看做法的话直接下载&#xff09;写在最前 Word 版本&#xff1a;2021 这个文章是…

[附源码]JAVA毕业设计技术的游戏交易平台(系统+LW)

[附源码]JAVA毕业设计技术的游戏交易平台&#xff08;系统LW&#xff09; 目运行 环境项配置&#xff1a; Jdk1.8 Tomcat8.5 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技…

Python使用GARCH,EGARCH,GJR-GARCH模型和蒙特卡洛模拟进行股价预测

全文下载链接&#xff1a;http://tecdat.cn/?p20678在本文中&#xff0c;预测股价已经受到了投资者&#xff0c;政府&#xff0c;企业和学者广泛的关注。然而&#xff0c;数据的非线性和非平稳性使得开发预测模型成为一项复杂而具有挑战性的任务&#xff08;点击文末“阅读原文…

HTML+CSS个人电影网页设计——电影从你的全世界路过(4页)带音乐特效

HTML实例网页代码, 本实例适合于初学HTML的同学。该实例里面有设置了css的样式设置&#xff0c;有div的样式格局&#xff0c;这个实例比较全面&#xff0c;有助于同学的学习,本文将介绍如何通过从头开始设计个人网站并将其转换为代码的过程来实践设计。 文章目录一、网页介绍一…

期末测试——H5方式练习题

期末考试——H5完成方式——练习题 前言 本练习训练了孩子们的栅格化布局&#xff0c;H5标签&#xff0c;CSS样式&#xff0c;并对列表与菜单进行了强化训练。使孩子们更好的掌握H5布局&#xff0c;为后面的框架教学打好基础。 素材下载地址&#xff1a; 链接&#xff1a;htt…

【学习记录】Dynaslam源代码魔改-替换MaskRCNN为YoloV5

这两天接了个小任务&#xff0c;需求是替换Dynaslam里面的动态物体识别模块&#xff0c;将MaskRCNN换为YoloV5&#xff0c;这里记录一下过程中遇见的问题。 一、运行Dynaslam Dynaslam本身是一个基于ORBSLAM2的视觉SLAM框架&#xff0c;论文并没有仔细看过&#xff0c;简单来…

HTML5期末大作业:基于html+css+javascript+jquery实现藏族文化15页【学生网页设计作业源码】

&#x1f389;精彩专栏推荐 &#x1f4ad;文末获取联系 ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业&#xff1a; 【&#x1f4da;毕设项目精品实战案例 (10…

免费搜索vs付费搜索:百度谷歌的有力竞争者刚刚出现!

北大出版社&#xff0c;人工智能原理与实践 人工智能和数据科学从入门到精通 详解机器学习深度学习算法原理 人工智能原理与实践 全面涵盖人工智能和数据科学各个重要体系经典 搜索引擎的游戏刚刚被改变&#xff01; 人们对昨天&#xff08;12月1号&#xff09;OpenAI 发布…

UltraISO 制作U盘启动盘

1、 从Ubuntu官网http://cn.ubuntu.com/download/下载系统的iso文件 用来制作的U盘需要是FAT32格式的&#xff0c;可以通过格式化U盘更改&#xff08;注意&#xff0c;如果U盘已经写入过隐藏分区了&#xff0c;需要在磁盘管理里面删除相关隐藏分区然后合并为一个大分区后…

[附源码]Python计算机毕业设计Django基于协同过滤的资讯推送平台

项目运行 环境配置&#xff1a; Pychram社区版 python3.7.7 Mysql5.7 HBuilderXlist pipNavicat11Djangonodejs。 项目技术&#xff1a; django python Vue 等等组成&#xff0c;B/S模式 pychram管理等等。 环境需要 1.运行环境&#xff1a;最好是python3.7.7&#xff0c;…

MyBatis动态SQL多表操作

动态SQL if-where标签 <select id"selByCondition" resultMap"rm">select *from mybatis<where><if test"status !null">and STATUS#{STATUS}</if><if test"companyName !null and companyName !">an…

idea10个常用的Debug技巧

文章目录一 回到上一步二 字段断点三 Stream调试四 表达式结果查看五 debug筛选条件六 异常断点七 远程调试八 强制返回九 运行时修改变量十 多线程调试重用快捷键一 回到上一步 进行代码调试的过程中&#xff0c;有的时候由于自己点击下一步的速度比较快&#xff0c;可能之前…

世界连续动作预测模型-方向模型

🍿*★,*:.☆欢迎您/$:*.★* 🍿 正文 <

[附源码]Python计算机毕业设计Django基于java的社区管理系统

项目运行 环境配置&#xff1a; Pychram社区版 python3.7.7 Mysql5.7 HBuilderXlist pipNavicat11Djangonodejs。 项目技术&#xff1a; django python Vue 等等组成&#xff0c;B/S模式 pychram管理等等。 环境需要 1.运行环境&#xff1a;最好是python3.7.7&#xff0c;…

云原生k8s的金箍棒

如果云原生是我们喜欢的《西游记》中孙悟空&#xff0c;那Autoscaling就是云原生手中的金箍棒。想象一下&#xff0c;没有金箍棒的孙悟空&#xff0c;还能到处降妖伏魔么&#xff1f;还能成为斗战圣佛吗&#xff1f; Autoscaling 根据需要随时动态扩缩容&#xff0c;有了它&…

TsingHua:FPT: Improving Prompt Tuning Efficiency via Progressive Training

这篇文章我觉得做的也挺有意思的。 是在探究提示学习中渐进式的更新参数比一窝蜂的直接更新参数效果要佳。 开头 是从比较PT&#xff08;prompt learning和fine tuning&#xff09;开始的。为什么PT的收敛速度慢&#xff0c;训练效率低下&#xff1f; 因为PT中中可训练的参数…

[附源码]计算机毕业设计新冠疫苗接种预约系统Springboot程序

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

第15章 基数法则

第15章 基数法则 15.1 通过其他计数来计算当前计数 如何计算拥挤的房间里有多少人?你可以数人头&#xff0c;因为一个人就只有一个头。或者&#xff0c;也可以数耳朵&#xff0c;然后除以2。我们往往可以通过对其他事物计数进而计算当前计数。 最直截了当的通过查找一种事物…

Kotlin高仿微信-第34篇-支付-向商家付款(二维码)

Kotlin高仿微信-项目实践58篇详细讲解了各个功能点&#xff0c;包括&#xff1a;注册、登录、主页、单聊(文本、表情、语音、图片、小视频、视频通话、语音通话、红包、转账)、群聊、个人信息、朋友圈、支付服务、扫一扫、搜索好友、添加好友、开通VIP等众多功能。 Kotlin高仿…

这么简单,还不会使用java8 stream流的map()方法吗?

一、前言 在日常的开发工作中经常碰到要处理list中数据的问题&#xff0c;比如从数据库中查出了很多学生&#xff0c;由于一些原因需要在内存中找出这些学生中的所有姓名&#xff0c;或者把名为“王五”的语文成绩暂时修改为“100”&#xff0c;这些问题怎么处理呐&#xff0c…