一般簡單的查詢,可以使用,findByName,findById等方法,但是當這種方法越來越多的時候,就會顯得特別冗雜,這個時候就需要把方法進行合併,spring data jpa提供了一種Query by Example的方法。
- 當僅使用簡單查詢的時候
@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 ?
- 上述內容可以看到,由於修改時間和創建時間有默認值,也當成了查詢條件,這並不是我們想要的,於是做出修改如下,
@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 ?
- 但是有很多時候我們查詢的時候是需要模糊查詢的,這個時候需要再次對需要模糊查詢的字段進行特殊的處理
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/
更多有趣,好玩的信息請關注我的微信公衆號!
`