在我們平時的工作中,查詢列表在我們的系統中基本隨處可見,那么我們如何使用jpa進行多條件查詢以及查詢列表分頁呢?下面我將介紹兩種多條件查詢方式。
1、引入起步依賴
2、對thymeleaf和jpa進行配置
打開application.yml,添加以下參數,以下配置在之前的文章中介紹過,此處不做過多說明
spring: thymeleaf: cache: true check-template-location: true content-type: text/html enabled: true encoding: utf-8 mode: HTML5 prefix: classpath:/templates/ suffix: .html excluded-view-names: template-resolver-order: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/restful?useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: root initialize: true init-db: true jpa: database: mysql show-sql: true hibernate: ddl-auto: update naming: strategy: org.hibernate.cfg.ImprovedNamingStrategy
3、編寫實體Bean
4、編寫Repository接口
5、抽象service層
首先抽象出接口
實現接口
此處我定義了兩個接口,findBookNoCriteria是不帶查詢條件的,findBookCriteria是帶查詢條件的。在此處介紹一下上面提到的自定義Repository繼承的兩個接口,如果你的查詢列表是沒有查詢條件,只是列表展示和分頁,只需繼承JpaRepository接口即可,但是如果你的查詢列表是帶有多個查詢條件的話則需要繼承JpaSpecificationExecutor接口,這個接口里面定義的多條件查詢的方法。當然不管繼承哪個接口,當你做分頁查詢時,都是需要調用findAll方法的,這個方法是jap定義好的分頁查詢方法。
findBookCriteria方法也可以使用以下方法實現,大家可以自行選擇
6、編寫Controller
針對有查詢條件和無查詢條件,我們分別編寫一個Controller,默認每頁顯示5條,如下
7、編寫頁面
首先我們編寫一個通用的分頁頁面,新建一個叫page.html的頁面
針對無查詢條件的接口,創建一個名為index1.html的頁面並引入之前寫好的分頁頁面,如下
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"/> <title>Title</title> <script type="text/javascript" th:src="https://my.oschina.net/wangxincj/blog/@{/jquery-1.12.3.min.js}"></script> <script type="text/javascript" th:src="https://my.oschina.net/wangxincj/blog/@{/bootstrap/js/bootstrap.min.js}"></script> <link type="text/css" rel="stylesheet" th:href="https://my.oschina.net/wangxincj/blog/@{/bootstrap/css/bootstrap-theme.min.css}"/> <link type="text/css" rel="stylesheet" th:href="https://my.oschina.net/wangxincj/blog/@{/bootstrap/css/bootstrap.css}"/> </head> <body> <table class="table table-hover"> <thead> <tr> <th>ID</th> <th>name</th> <th>isbn</th> <th>author</th> </tr> </thead> <tbody> <tr th:each="obj : ${datas}"> <td th:text="${obj.id}">${obj.id}</td> <td th:text="${obj.name}">${obj.name}</td> <td th:text="${obj.isbn}">${obj.isbn}</td> <td th:text="${obj.name}">${obj.author}</td> </tr> </tbody> </table> <div th:include="page :: pager" th:remove="tag"></div> </body> </html>
針對有查詢條件的接口,創建一個名為index2.html的頁面並引入之前寫好的分頁頁面,如下 <!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"/> <title>Title</title> <script type="text/javascript" th:src="https://my.oschina.net/wangxincj/blog/@{/jquery-1.12.3.min.js}"></script> <script type="text/javascript" th:src="https://my.oschina.net/wangxincj/blog/@{/bootstrap/js/bootstrap.min.js}"></script> <link type="text/css" rel="stylesheet" th:href="https://my.oschina.net/wangxincj/blog/@{/bootstrap/css/bootstrap-theme.min.css}"/> <link type="text/css" rel="stylesheet" th:href="https://my.oschina.net/wangxincj/blog/@{/bootstrap/css/bootstrap.css}"/> </head> <body> <form th:action="@{/queryBook/findBookQuery}" th:object="${bookQuery}" th:method="get"> <div class="form-group"> <label class="col-sm-2 control-label" >name</label> <div class="col-sm-4"> <input type="text" class="form-control" id="name" placeholder="請輸入名稱" th:field="*{name}"/> </div> <label class="col-sm-2 control-label">isbn</label> <div class="col-sm-4"> <input type="text" class="form-control" id="isbn" placeholder="請輸ISBN" th:field="*{isbn}"/> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label" >author</label> <div class="col-sm-4"> <input type="text" class="form-control" id="author" placeholder="請輸author" th:field="*{author}"/> </div> <div class="col-sm-4"> <button class="btn btn-default" type="submit" placeholder="查詢">查詢</button> </div> </div> </form> <table class="table table-hover"> <thead> <tr> <th>ID</th> <th>name</th> <th>isbn</th> <th>author</th> </tr> </thead> <tbody> <tr th:each="obj : ${datas}"> <td th:text="${obj.id}">${obj.id}</td> <td th:text="${obj.name}">${obj.name}</td> <td th:text="${obj.isbn}">${obj.isbn}</td> <td th:text="${obj.name}">${obj.author}</td> </tr> </tbody> </table> <div th:include="page :: pager" th:remove="tag"></div> </body> </html>
ok!代碼都已經完成,我們將項目啟動起來,看一下效果。大家可以往數據庫中批量插入一些數據,訪問http://localhost:8080/queryBook/findBookNoQuery,顯示如下頁面
訪問http://localhost:8080/queryBook/findBookQuery,顯示頁面如下,可以輸入查詢條件進行帶條件的分頁查詢:
ok!以上便是一個簡單的jap分頁查詢功能的實現。