JPA Specification複雜查詢及排序

1、前言

剛使用spring-data-jpa,遇到很多難題,網上查了不少資料,發現講jpa的很少,發個我剛作過的接口的過程吧。前端

2、需求

看到圖了嗎?須要實現搜索以及各類字段的排序還要分頁,還有可能有選擇各類條件的下拉列表,是否是很變態?spring

3、開始了

1、dao

須要先處理dao層,這裏喜歡叫repository。作一個實體類的dao層接口,繼承JpaSpecificationExecutor,再寫一個查詢接口。編碼

2、service

在這裏主要處理的是查詢條件,我這裏是搜索功能的模糊查詢,固然若是有更多的查詢也能夠添加進這裏。這裏須要注意的是specification。spa

3、排序

須要先建一個輔助的實體類,屬性名我取和須要排序的實體類同樣的名字,可是注意屬性都是String類型的啊。後面細說,先上我建的輔助類。3d

@Data
public class DeptSort {

private String id;//編碼

private String name;//名稱

private String highDeptName;//上級部門

private String principal;//負責人

private String deptType;//部門類型

private String enable;//啓用
}
字段都是須要排序的字段,這是爲了好區分,叫別的也能夠。
下面是controller層,排序功能的具體實現。
public ResponseModel table(@RequestParam("search")String search,
@RequestParam("pageNumber")Integer pageNumber,
@RequestParam("pageSize")Integer pageSize,
@RequestBody DeptSort deptSort){
ResponseModel model = null;
try {
List<Sort.Order> orders = new ArrayList<Sort.Order>();
if (StringUtils.isNotBlank(deptSort.getId())){
orders.add(new Sort.Order(Sort.Direction.fromString(deptSort.getId()),"id"));
}
if (StringUtils.isNotBlank(deptSort.getName())){
orders.add(new Sort.Order(Sort.Direction.fromString(deptSort.getName()),"name"));
}
if (StringUtils.isNotBlank(deptSort.getHighDeptName())){
orders.add(new Sort.Order(Sort.Direction.fromString(deptSort.getHighDeptName()),"highDeptName"));
}
if (StringUtils.isNotBlank(deptSort.getPrincipal())){
orders.add(new Sort.Order(Sort.Direction.fromString(deptSort.getPrincipal()),"principal"));
}
if (StringUtils.isNotBlank(deptSort.getDeptType())){
orders.add(new Sort.Order(Sort.Direction.fromString(deptSort.getDeptType()),"deptType"));
}
if (StringUtils.isNotBlank(deptSort.getEnable())){
orders.add(new Sort.Order(Sort.Direction.fromString(deptSort.getEnable()),"enable"));
}
//orders不能爲空,因此若是爲空設置按id排序
        if (orders.size() == 0){
orders.add(new Sort.Order(Sort.Direction.ASC,"id"));
}
        Sort sort = new Sort(orders);
Pageable pageable = new PageRequest(pageNumber,pageSize,sort);
Page<Businessdept> all = service.findAll(search, pageable);
model = ResponseModel.getSuccessResponseModel().setData(all);
}catch (Exception e){
e.printStackTrace();
model = ResponseModel.getFailedResponseModel();
}
return model;
}

須要的參數有搜索內容search,還有DeptSort輔助類。首先創建blog

List<Sort.Order> orders = new ArrayList<Sort.Order>();集合,而後if判斷將參數加入集合。
須要說明的是相似
orders.add(new Sort.Order(Sort.Direction.fromString(deptSort.getEnable()),"enable"))語句,「enable」是須要查詢的
Businessdept裏的字段,不是輔助類的,固然這裏個人輔助類和Businessdept類一致,可是不同的同窗須要注意了。

 4、前端

對於前端傳遞的參數有什麼要求呢?排序

deptSort的各個屬性的參數只能限定兩種asc和desc,即升序和降序。上圖的功能需求只須要傳deptSort裏的一個屬性就能夠了,這裏傳兩個參數演示一下。繼承

查詢成功的數據不展現了,給你們看一個後臺的SQL語句接口

Hibernate:
/* select
count(generatedAlias0)
from
Businessdept as generatedAlias0
where
(
generatedAlias0.name like :param0
)
and (
generatedAlias0.deleteIs=1
) */ select
count(businessde0_.id) as col_0_0_
from
t_department businessde0_
where
(
businessde0_.name like ?
)
and businessde0_.delete_is=1
Hibernate:
/* select
generatedAlias0
from
Businessdept as generatedAlias0
where
(
generatedAlias0.name like :param0
)
and (
generatedAlias0.deleteIs=1
)
order by
generatedAlias0.deptType asc,
generatedAlias0.enable desc */ select
businessde0_.id as id1_3_,
businessde0_.delete_is as delete_i2_3_,
businessde0_.dept_type as dept_typ3_3_,
businessde0_.enable as enable4_3_,
businessde0_.high_dept_id as high_dep5_3_,
businessde0_.high_dept_name as high_dep6_3_,
businessde0_.name as name7_3_,
businessde0_.principal as principa8_3_
from
t_department businessde0_
where
(
businessde0_.name like ?
)
and businessde0_.delete_is=1
order by
businessde0_.dept_type asc,
businessde0_.enable desc limit ?ip

能夠看到條件查詢,升序、降序都有。

4、結束語

好了,我是我發的第一個帖子,請多多支持,最好關注我。

相關文章
相關標籤/搜索