spring data jpa使用Pageable,ExampleMatcher進行分頁多條件查詢

一般簡單的查詢,可以使用,findByName,findById等方法,但是當這種方法越來越多的時候,就會顯得特別冗雜,這個時候就需要把方法進行合併,spring data jpa提供了一種Query by Example的方法。

  1. 當僅使用簡單查詢的時候
@Override
public Page queryByPage(T t, Pageable pageable) {
    Example<T> example = Example.of(t);
    return baseDao.findAll(example, pageable);
}

這種查詢會將實體中所有不爲空的屬性(包括父類的)都作爲查詢條件

SELECT roleentity0_.id AS id1_7_, roleentity0_.delete_flag AS delete_f2_7_, roleentity0_.system_id AS system_i3_7_, roleentity0_.create_time AS create_t4_7_, roleentity0_.create_user_id AS create_u5_7_, roleentity0_.update_time AS update_t6_7_, roleentity0_.update_user AS update_u7_7_, roleentity0_.content AS content8_7_, roleentity0_.org_id AS org_id9_7_, roleentity0_.role_name AS role_na10_7_, roleentity0_.role_type AS role_ty11_7_, roleentity0_.weight AS weight12_7_
FROM dafangzi_role roleentity0_
WHERE roleentity0_.update_time=? AND roleentity0_.create_time=? AND roleentity0_.delete_flag=?
ORDER BY roleentity0_.update_time DESC
LIMIT ?
  1. 上述內容可以看到,由於修改時間和創建時間有默認值,也當成了查詢條件,這並不是我們想要的,於是做出修改如下,
@Override
public Page queryByPage(T t, Pageable pageable) {
    //Example<T> example = Example.of(t);
    ExampleMatcher matcher = ExampleMatcher.matching().withIgnorePaths("createTime", "updateTime");
    Example<T> example = Example.of(t, matcher);
    return baseDao.findAll(example, pageable);
}

再查看查詢結果,就是我們想要的結果了

SELECT roleentity0_.id AS id1_7_, roleentity0_.delete_flag AS delete_f2_7_, roleentity0_.system_id AS system_i3_7_, roleentity0_.create_time AS create_t4_7_, roleentity0_.create_user_id AS create_u5_7_, roleentity0_.update_time AS update_t6_7_, roleentity0_.update_user AS update_u7_7_, roleentity0_.content AS content8_7_, roleentity0_.org_id AS org_id9_7_, roleentity0_.role_name AS role_na10_7_, roleentity0_.role_type AS role_ty11_7_, roleentity0_.weight AS weight12_7_
FROM dafangzi_role roleentity0_
WHERE roleentity0_.delete_flag=?
ORDER BY roleentity0_.update_time DESC
LIMIT ?
  1. 但是有很多時候我們查詢的時候是需要模糊查詢的,這個時候需要再次對需要模糊查詢的字段進行特殊的處理
ExampleMatcher matcher = ExampleMatcher.matching().withIgnorePaths("createTime", "updateTime").withMatcher("roleName", match -> match.contains());
Example<RoleEntity> example = Example.of(roleEntity, matcher);
Page<RoleEntity> entityPage = roleService.queryByPage(example, PageRequest.of(page-1, limit, sort));

這樣,就變成模糊查詢了

SELECT roleentity0_.id AS id1_7_, roleentity0_.delete_flag AS delete_f2_7_, roleentity0_.system_id AS system_i3_7_, roleentity0_.create_time AS create_t4_7_, roleentity0_.create_user_id AS create_u5_7_, roleentity0_.update_time AS update_t6_7_, roleentity0_.update_user AS update_u7_7_, roleentity0_.content AS content8_7_, roleentity0_.org_id AS org_id9_7_, roleentity0_.role_name AS role_na10_7_, roleentity0_.role_type AS role_ty11_7_, roleentity0_.weight AS weight12_7_
FROM dafangzi_role roleentity0_
WHERE (roleentity0_.role_name LIKE ?) AND roleentity0_.delete_flag=?
ORDER BY roleentity0_.update_time DESC
LIMIT ?

附其它字符串處理的操作
這裏寫圖片描述
參考鏈接:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/

更多有趣,好玩的信息請關注我的微信公衆號!

這裏寫代碼片
`

發佈了40 篇原創文章 · 獲贊 5 · 訪問量 9129
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章