JPA分页查询与条件分页查询
生活随笔
收集整理的這篇文章主要介紹了
JPA分页查询与条件分页查询
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
情有獨鐘的JPA
平時在寫一些小項目時,比較喜歡引用 Spring Data Jpa,其實還是圖他寫代碼快~
在日常的開發工作中,分頁列表查詢基本是隨處可見,下面一起看一下如何使用 jpa 進行多條件查詢以及查詢列表分頁呢?
關于JPA的使用
關于 jpa 的使用,下面2步簡單過一下,詳細資料,小伙伴自行搜索一下吧~
1、導入依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency>mysql、web、druid...... </dependency>2、配置yml
圖方便直接貼代碼了:
spring:#?數據源datasource:url:?jdbc:mysql://127.0.0.1:3306/tmax?useUnicode=true&characterEncoding=utf-8&useSSL=falseusername:?rootpassword:?1234type:?com.alibaba.druid.pool.DruidDataSourcedriverClassName:?com.mysql.jdbc.Driverjpa:#?操作數據庫時顯示sql語句show-sql:?true#?自動生成表結構generate-ddl:?truehibernate:ddl-auto:?nonedatabase-platform:?org.hibernate.dialect.MySQL57Dialect分頁查詢
我們了解 jpa 基本是不用去寫 sql 的,繼承 JpaRepository 即可,同樣也提供給了我們分頁查詢的方法:
舉例:
Page<VideoCategory>?findByCondition(SearchVo?searchVo,?Pageable?pageable);通過傳入一個遵循 pageale 協議的對象來獲取某一頁的數據,通過源碼查看,發現 Pageable 是一個接口,提供了分頁一組方法的聲明,如第幾頁,每頁多少條記錄,排序信息等,部分方法如下:
int?getPageNumber();int?getPageSize();int?getOffset();Sort?getSort();Pageable?next();Pageable?previousOrFirst();Pageable?first();boolean?hasPrevious();通過這些方法我們可以構造我們的 pageable 對象,需要注意的是 jpa 在構造頁碼初始時,是從 0 開始的。
廢話不多說,來看一段代碼吧:
1. impl
????@Overridepublic?Page<VideoCategory>?findByCondition(VideoCategory?videoCategory,?SearchVo?searchVo,?Pageable?pageable)?{return?videoCategoryDao.findAll(new?Specification<VideoCategory>()?{@Nullable@Overridepublic?Predicate?toPredicate(Root<VideoCategory>?root,?CriteriaQuery<?>?cq,?CriteriaBuilder?cb)?{//?可添加你的其他搜索過濾條件?默認已有創建時間過濾Path<Date>?createTimeField=root.get("createTime");Path<String>?categoryIdField=root.get("categoryId");List<Predicate>?list?=?new?ArrayList<Predicate>();//創建時間if(StrUtil.isNotBlank(searchVo.getStartDate())&&StrUtil.isNotBlank(searchVo.getEndDate())){Date?start?=?DateUtil.parse(searchVo.getStartDate());Date?end?=?DateUtil.parse(searchVo.getEndDate());list.add(cb.between(createTimeField,?start,?DateUtil.endOfDay(end)));}//?視頻分類if(StrUtil.isNotBlank(videoCategory.getCategoryId())){????????list.add(cb.equal(categoryIdField,videoCategory.getCategoryId()));}Predicate[]?arr?=?new?Predicate[list.size()];cq.where(list.toArray(arr));return?null;}},?pageable);}2. controller
????@RequestMapping(value?=?"/getByCondition",?method?=?RequestMethod.GET)@ApiOperation(value?=?"多條件分頁獲取")public?Result<Page<VideoCategory>>?getByCondition(@ModelAttribute?VideoCategory?videoCategory,@ModelAttribute?SearchVo?searchVo,@ModelAttribute?PageVo?pageVo){Page<VideoCategory>?page?=?videoCategoryService.findByCondition(videoCategory,?searchVo,?PageUtil.initPage(pageVo));return?new?ResultUtil<Page<VideoCategory>>().setData(page);}3. PageUtil
public?static?Pageable?initPage(PageVo?page){Pageable?pageable?=?null;int?pageNumber?=?page.getPageNumber();int?pageSize?=?page.getPageSize();String?sort?=?page.getSort();String?order?=?page.getOrder();if(pageNumber<1){pageNumber?=?1;}if(pageSize<1){pageSize?=?10;}if(StrUtil.isNotBlank(sort))?{Sort.Direction?d;if(StrUtil.isBlank(order))?{d?=?Sort.Direction.DESC;}?else?{d?=?Sort.Direction.valueOf(order.toUpperCase());}Sort?s?=?new?Sort(d,?sort);pageable?=?PageRequest.of(pageNumber-1,?pageSize,?s);}?else?{pageable?=?PageRequest.of(pageNumber-1,?pageSize);}return?pageable;} posted @ 2019-05-06 00:02 niceyoo 閱讀(...) 評論(...) 編輯 收藏總結
以上是生活随笔為你收集整理的JPA分页查询与条件分页查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HP惠普打印机双面打印及常规操作
- 下一篇: .net的轻量级ORM -- PetaP