SpringMVC+Spring Data JPA +Bootstrap 分頁實現和模糊查詢分頁
本文將介紹使用 SpringMVC + Spring Data JPA 實現分頁功能。
Spring Data JSP 裡面一個一個 Pageable 的類可以幫我們實現分頁,非常好用。
先看效果圖
使用者列表分頁
使用者查詢(根據使用者名稱,暱稱,email,個人主頁,手機號)模糊查詢 分頁
如果是首頁不顯示上一頁,如果是尾頁不顯示下一頁。
分頁的API
使用 Spirng Data JPA 的 Page 替代之前的 List。
下面是返回給前臺的資料
- // 20171202185123
- // http://localhost:8090/admin/user/list
- {
- "content": [
- {
- "id": 1,
- "username": "liubei",
- "password": null,
- "nickname": "劉玄德",
- "email": "liubei@shu.com",
- "avatar": "/uploads/2017/12/劉備.png",
- "website": "http://liubei.com",
- "phone": "15711111111",
- "createTime": 1512193914000,
- "status": 1
- },
- {
- "id": 2,
- "username": "lv007",
- "password": "007",
- "nickname": "呂玲綺",
- "email": "007@san.com",
- "avatar": "/uploads/2017/12/呂玲綺.png",
- "website": "http://007.com",
- "phone": "15711111112",
- "createTime": 1512194060000,
- "status": 1
- },
- {
- "id": 3,
- "username": "mayunlu",
- "password": "123",
- "nickname": "馬雲祿",
- "email": "mayunlu@san.com",
- "avatar": "/uploads/2017/12/馬雲祿.png",
- "website": "http://mayunlu.com",
- "phone": "13533333333",
- "createTime": 1512194114000,
- "status": 1
- }
- ],
- "last": false,
- "totalElements": 29,
- "totalPages": 10,
- "size": 3,
- "number": 0,
- "sort": null,
- "first": true,
- "numberOfElements": 3
- }
可以看到能獲得總共記錄數totalElements,總頁數totalPages,每頁顯示的記錄數size,當前頁碼number(從0開始計),排序規則sort,是否為首頁first,是否為尾頁last,當前頁的記錄數。content 裡的是記錄內容
後臺程式碼
1、dao 層
UserDao.java
- package com.liuyanzhao.blog.dao;
- import com.liuyanzhao.blog.entity.User;
- 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;
- /**
- * @author 言曌
- * @date 2017/11/28 下午3:31
- */
- public interface UserDao extends JpaRepository<User, Integer> {
- //根據使用者名稱暱稱等資訊模糊查詢
- @Query("select u from User u where u.username like %?1% or u.nickname like %?1% or u.email like %?1% or u.website like %?1% or u.phone like %?1% or u.id = ?1")
- Page<User> findSearch(String query, Pageable pageable);
- }
2、Service 層
UserServie.java
- package com.liuyanzhao.blog.service;
- import com.liuyanzhao.blog.dto.UserDTO;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.Pageable;
- /**
- * @author 言曌
- * @date 2017/11/28 下午3:32
- */
- public interface UserService {
- //全部查詢
- Page<UserDTO> findAll(Pageable pageable);
- //根據用多種資訊查詢模糊使用者
- Page<UserDTO> findSearch(String username,Pageable pageable);
- }
UserServiceImpl.java
- package com.liuyanzhao.blog.service.Impl;
- import com.liuyanzhao.blog.converter.User2UserDTOConverter;
- import com.liuyanzhao.blog.dao.UserDao;
- import com.liuyanzhao.blog.dto.UserDTO;
- import com.liuyanzhao.blog.entity.User;
- import com.liuyanzhao.blog.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.PageImpl;
- import org.springframework.data.domain.Pageable;
- import org.springframework.stereotype.Service;
- import javax.transaction.Transactional;
- import java.util.List;
- /**
- * @author 言曌
- * @date 2017/11/28 下午3:32
- */
- @Service("userService")
- public class UserServiceImpl implements UserService {
- @Autowired
- private UserDao userDao;
- //分頁獲得列表
- @Override
- public Page<UserDTO> findAll(Pageable pageable) {
- Page<User> userPage = userDao.findAll(pageable);
- List<UserDTO> userDTOList = User2UserDTOConverter.convert(userPage.getContent());
- Page<UserDTO> userDTOPage = new PageImpl<UserDTO>(userDTOList,pageable,userPage.getTotalElements());
- return userDTOPage;
- }
- @Override
- public Page<UserDTO> findSearch(String query,Pageable pageable) {
- Page<User> userPage = userDao.findSearch(query,pageable);
- List<UserDTO> userDTOList = User2UserDTOConverter.convert(userPage.getContent());
- Page<UserDTO> userDTOPage = new PageImpl<UserDTO>(userDTOList,pageable,userPage.getTotalElements());
- return userDTOPage;
- }
- }
3、controller 層
UserController.java
- package com.liuyanzhao.blog.controller;
- import com.liuyanzhao.blog.dto.UserDTO;
- import com.liuyanzhao.blog.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.PageRequest;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.servlet.ModelAndView;
- import javax.servlet.http.HttpServletRequest;
- /**
- * @author 言曌
- * @date 2017/11/28 下午3:33
- */
- @Controller
- @RequestMapping(value = "/admin/user")
- public class UserController {
- @Autowired
- private UserService userService;
- @RequestMapping(value = "/list")
- public ModelAndView listUser(@RequestParam(value = "page",defaultValue = "1") Integer page,
- @RequestParam(value = "size",defaultValue = "3") Integer size) {
- ModelAndView modelAndView = new ModelAndView();
- PageRequest request = new PageRequest(page-1,size);
- Page<UserDTO> userDTOPage = userService.findAll(request);
- modelAndView.addObject("userDTOPage",userDTOPage);
- modelAndView.setViewName("/admin/user/list");
- return modelAndView;
- }
- @RequestMapping(value = "/search")
- public ModelAndView search(HttpServletRequest request,
- @RequestParam(value = "page",defaultValue = "1") Integer page,
- @RequestParam(value = "size",defaultValue = "3") Integer size) {
- ModelAndView modelAndView = new ModelAndView();
- String query = (request.getParameter("query")).trim();
- PageRequest pageRequest = new PageRequest(page-1,size);
- Page<UserDTO> userDTOPage = userService.findSearch(query,pageRequest);
- modelAndView.addObject("userDTOPage",userDTOPage);
- modelAndView.addObject("query",query);
- modelAndView.setViewName("/admin/user/list");
- return modelAndView;
- }
- }
預設一頁顯示3條記錄,預設當前頁為di
4、實體類
UserDTO.java
- package com.liuyanzhao.blog.dto;
- import java.util.Date;
- /**
- *
- * 資料傳輸物件(不要使用entity直接來傳資料)
- * @author 言曌
- * @date 2017/11/30 下午10:53
- */
- public class UserDTO {
- private Integer id;
- private String username;
- private String password;
- private String nickname;
- private String email;
- private String avatar;
- private String website;
- private String phone;
- private Date createTime;
- private Integer status;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getNickname() {
- return nickname;
- }
- public void setNickname(String nickname) {
- this.nickname = nickname;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getAvatar() {
- return avatar;
- }
- public void setAvatar(String avatar) {
- this.avatar = avatar;
- }
- public String getWebsite() {
- return website;
- }
- public void setWebsite(String website) {
- this.website = website;
- }
- public String getPhone() {
- return phone;
- }
- public void setPhone(String phone) {
- this.phone = phone;
- }
- public Date getCreateTime() {
- return createTime;
- }
- public void setCreateTime(Date createTime) {
- this.createTime = createTime;
- }
- public Integer getStatus() {
- return status;
- }
- public void setStatus(Integer status) {
- this.status = status;
- }
- }
前臺程式碼
list.jsp 部分程式碼
- <div id="content">
- <div class="table-responsive">
- <form class="form-inline" method="get" action="/admin/user/search">
- <div class="form-group">
- <label for="Search" class="sr-only">Search</label>
- <input type="text" name="query" class="form-control" id="Search" placeholder="Search"
- value="${query}">
- </div>
- <button type="submit" class="btn btn-default">查詢</button>
- <a href="/admin/user/add">
- <button type="button" class="btn btn-info">新增</button>
- </a>
- <button type="button" class="btn btn-danger" onclick="deleteUserMore()">批量刪除</button>
- </form>
- <br>
- <c:choose>
- <c:when test="${userDTOPage.numberOfElements > 0}">
- <table class="table table-bordered">
- <tr>
- <th><input type="checkbox" id="allSelect" onclick="DoCheck()"></th>
- <th>ID</th>
- <th>使用者名稱</th>
- <th>暱稱</th>
- <th>郵箱</th>
- <th>電話</th>
- <th>個人主頁</th>
- <th>建立時間</th>
- <th>狀態</th>
- <th>操作</th>
- </tr>
- <c:forEach var="user" items="${userDTOPage.content}">
- <tr>
- <td><input type="checkbox" name="ids" value="${user.id}"></td>
- <td>${user.id}</td>
- <td>${user.username}</td>
- <td>${user.nickname}</td>
- <td>${user.email}</td>
- <td>${user.phone}</td>
- <td><a href="${user.website}" target="_blank">${user.website}</a></td>
- <td>${user.createTime}</td>
- <td>
- <c:choose>
- <c:when test="${user.status==1}">
- <span class="text-success">正常</span>
- </c:when>
- <c:when test="${user.status==0}">
- <span class="text-danger">禁用</span>
- </c:when>
- <c:otherwise>
- ${user.status}
- </c:otherwise>
- </c:choose>
- </td>
- <td>
- <a href="/admin/user/profile/${user.id}">
- <button type="button" class="btn btn-success btn-xs">檢視</button>
- </a>
- <button type="button" class="btn btn-danger btn-xs"
- onclick="deleteUser(${user.id})">刪除
- </button>
- <a href="/admin/user/edit/${user.id}">
- <button type="button" class="btn btn-primary btn-xs">編輯</button>
- </a>
- </td>
- </tr>
- </c:forEach>
- </table>
- <%--總共頁數:${userDTOPage.totalPages} <br>--%>
- <%--記錄總數:${userDTOPage.totalElements} <br>--%>
- <%--當前頁號:${userDTOPage.number} <br>--%>
- <%--是否為首頁:${userDTOPage.first} <br>--%>
- <%--是否為尾頁:${userDTOPage.last} <br>--%>
- <%--每頁顯示的數量:${userDTOPage.numberOfElements} <br>--%>
- <%--分頁 start--%>
- <nav aria-label="Page navigation">
- <ul class="pagination">
- <c:choose>
- <c:when test="${userDTOPage.totalPages <= 3 }">
- <c:set var="begin" value="1"/>
- <c:set var="end" value="${userDTOPage.totalPages }"/>
- </c:when>
- <c:otherwise>
- <c:set var="begin" value="${userDTOPage.number+1-1 }"/>
- <c:set var="end" value="${userDTOPage.number+1 + 2}"/>
- <c:if test="${begin < 2 }">
- <c:set var="begin" value="1"/>
- <c:set var="end" value="3"/>
- </c:if>
- <c:if test="${end > userDTOPage.totalPages}">
- <c:set var="begin" value="${userDTOPage.totalPages-2 }"/>
- <c:set var="end" value="${userDTOPage.totalPages}"/>
- </c:if>
- </c:otherwise>
- </c:choose>
- <%--如果當前為首頁,隱藏上一頁--%>
- <c:choose>
- <c:when test="${userDTOPage.first}">
- <%--當前頁為第一頁,隱藏上一頁按鈕--%>
- </c:when>
- <c:otherwise>
- <li>
- <a href="?page=${userDTOPage.number}" aria-label="Previous">
- <span aria-hidden="true">«</span>
- </a>
- </li>
- </c:otherwise>
- </c:choose>
- <%--顯示第一頁的頁碼--%>
- <c:if test="${begin >= 2 }">
- <li><a href="?page=1">1</a></li>
- </c:if>
- <%--顯示點點點--%>
- <c:if test="${begin > 2 }">
- <li class="disabled"><a>…</a></li>
- </c:if>
- <%--列印 頁碼--%>
- <c:forEach begin="${begin }" end="${end }" var="i">
- <c:choose>
- <c:when test="${i eq userDTOPage.number+1}">
- <li class="active"><a href="?page=${i}">${i}<span
- class="sr-only">(current)</span></a></li>
- </c:when>
- <c:otherwise>
- <li><a href="?page=${i}">${i}</a></li>
- </c:otherwise>
- </c:choose>
- </c:forEach>
- <%-- 顯示點點點 --%>
- <c:if test="${end < userDTOPage.totalPages-1 }">
- <li class="disabled"><a>…</a></li>
- </c:if>
- <%-- 顯示最後一頁的數字 --%>
- <c:if test="${end < userDTOPage.totalPages}">
- <li>
- <a href="?page=${userDTOPage.totalPages}">${userDTOPage.totalPages}</a>
- </li>
- </c:if>
- <%--如果當前頁為尾頁,隱藏下一頁--%>
- <c:choose>
- <c:when test="${userDTOPage.number+1 eq userDTOPage.totalPages}">
- <%--到了尾頁隱藏,下一頁按鈕--%>
- </c:when>
- <c:otherwise>
- <li>
- <a href="?page=${userDTOPage.number+2}" aria-label="Next">
- <span aria-hidden="true">»</span>
- </a>
- </li>
- </c:otherwise>
- </c:choose>
- </ul>
- </nav>
- <%--分頁 end--%>
- </c:when>
- <c:otherwise><%--如果沒有文章--%>
- <div class="alert alert-warning">
- <a href="#" class="close" data-dismiss="alert">
- ×
- </a>
- <strong>警告!</strong>這裡什麼都沒有。哼,小壞壞!
- </div>
- </c:otherwise>
- </c:choose>
- </div>
- </div>
search.jsp
這個search.jsp 和 list.jsp 基本相同,只是下面的分頁連結不同
一個是 <a href="?page=${i}">${i}</a>
一個是 <a href="?query=${query}&page=${i}">${i}</a>
這裡就不貼程式碼了
其中分頁部分需要注意這個就行了
總共頁數:${userDTOPage.totalPages} 記錄總數:${userDTOPage.totalElements} 當前頁號:${userDTOPage.number} 是否為首頁:${userDTOPage.first} 是否為尾頁:${userDTOPage.last} 每頁顯示的數量:${userDTOPage.numberOfElements}
相關文章
- spring-data-jpa + SpringBoot + bootstrapTable 後端分頁 模糊查詢Spring BootAPT後端
- 直播app開發,使用koa和MongoDB實現分頁和模糊查詢APPMongoDB
- indexdb實現分頁查詢Index
- c# winform 實現分頁查詢C#ORM
- SSM框架實現分頁查詢例子SSM框架
- MYSQL SQLServer分頁查詢的實現MySqlServer
- 如何優雅地實現分頁查詢
- Hibernate實現分頁查詢的原理
- Elasticsearch 分頁查詢Elasticsearch
- ssh 分頁查詢
- oracle分頁查詢Oracle
- SpringMVC+Spring Data JPA實現增刪改查操作SpringMVC
- iOS程式設計師利用分頁和模糊查詢技術實現一個App介面iOS程式設計師APP
- Spring Data Jpa 複雜查詢總結 (多表關聯 以及 自定義分頁 )Spring
- 老聲常談,分頁查詢的實現就是不分頁
- SSH整合實現分頁查詢(兩種方式)
- Spring JPA 分頁Spring
- MySQL的分頁查詢MySql
- ThinkPhp框架:分頁查詢PHP框架
- 分頁查詢優化優化
- NET 集合分頁查詢
- MySQL 多表查詢分頁MySql
- 分頁查詢重構
- Oracle的分頁查詢Oracle
- Oracle分頁查詢格式Oracle
- Lucene的分頁查詢
- 分庫分表後的分頁查詢
- MySQL——優化巢狀查詢和分頁查詢MySql優化巢狀
- golang通過mysql語句實現分頁查詢GolangMySql
- golang透過mysql語句實現分頁查詢GolangMySql
- hibernate 通用分頁,查詢分頁的泛型類泛型
- django 自定義分頁與bootstrap分頁結合Djangoboot
- 通用分頁-基於bootstrap和jQuerybootjQuery
- AntDesignBlazor示例——分頁查詢Blazor
- MySQL分頁查詢優化MySql優化
- Oracle分頁查詢格式(十三)Oracle
- Oracle分頁查詢格式(十二)Oracle
- Oracle分頁查詢格式(十一)Oracle