一.JPA使用過程中的問題
JPA常用的查詢方法,用實體對應的repository的執行find方法,查詢都是實體的全部字段或者其中的單個字段。
如果對一個字段進行分組查詢時,會出現問題,這里分享一個自定義查詢方法。
二.解決問題
一.分組查詢
表數據
JPA對應實體
結果集VO
進行分組查詢
-
-
-
-
public class JPAController {
-
-
-
private EntityManager entityManager;
-
-
-
-
-
public List<StudentVO> jpa(){
-
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
-
CriteriaQuery<Tuple> cq = cb.createTupleQuery();
-
Root<Student> root = cq.from(Student.class); //具體實體的Root
-
root.alias( "o");//別名
-
cq.multiselect(root.get( "name").alias("name"),root.get("age").alias("age"));
-
-
Predicate where = cb.conjunction();
-
//增加查詢條件
-
cq.where(where).groupBy(root.get( "name"),root.get("age"));
-
List<Tuple> tuples = entityManager.createQuery(cq).getResultList();
-
//結果集
-
List<StudentVO> result = new ArrayList<>();
-
for(Tuple tuple : tuples){
-
result.add( new StudentVO(tuple.get(0).toString(),tuple.get(1).toString()));
-
}
-
return result;
-
}
-
}
結果集截圖(swagger文檔接口調試)
根據自己的情況,寫對應的JPA過濾代碼。
業務場景:
-
/**
-
* 查詢月份集合(JPA分組)
-
* @param type
-
* @return
-
*/
-
public Integer countNum(MonthlyParamVO param,String type,String isF ,Collection<Integer> functionSonList,Collection<Integer> orgIds){
-
//初始化結果集
-
List<String> monthList = new ArrayList<String>();
-
//初始化JPA查詢
-
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
-
CriteriaQuery<Tuple> cq = cb.createTupleQuery();
-
Root<AttendanceMonthlyReportEntity> root = cq.from(AttendanceMonthlyReportEntity.class);
-
//加條件
-
if("missDays".equals(type)){
-
cq.multiselect(cb.sum(root.get( "missDays")));
-
} else if("absentDays".equals(type)){
-
cq.multiselect(cb.sum(root.get( "absentDays")));
-
} else if("lateEarly".equals(type)){
-
cq.multiselect(cb.sum(root.get( "lateEarly")));
-
} else if("notPunched".equals(type)){
-
cq.multiselect(cb.sum(root.get( "notPunched")));
-
}
-
//查詢條件where
-
List<Predicate> predicates = new ArrayList<>();
-
if(StringUtils.isNotBlank(param.getZhName())){
-
predicates.add(cb.equal(root.get( "zhName"),param.getZhName()));
-
}
-
if(null!=param.getOrgIds()&¶m.getOrgIds().size()!=0){
-
predicates.add(root.get( "orgId").in(orgIds));
-
}
-
if(StringUtils.isNotBlank(param.getYear())&&StringUtils.isBlank(param.getMonth())){
-
predicates.add(cb.equal(root.get( "attendanceMonth"),param.getYear()+"%"));
-
}
-
if(StringUtils.isNotBlank(param.getZhName())&&StringUtils.isNotBlank(param.getMonth())){
-
predicates.add(cb.equal(root.get( "attendanceMonth"),param.getYear()+"-"+param.getMonth()));
-
}
-
if("Y".equals(isF)){
-
//加入需求限制
-
predicates.add(cb.or(root.get( "orgId").in(functionSonList),cb.like(root.get("positionLevel"),"F")));
-
}
-
cq.where(predicates.toArray( new Predicate[predicates.size()]));
-
-
List<Tuple> tuples = entityManager.createQuery(cq).getResultList();
-
//求和
-
Integer count = 0;
-
for(Tuple tuple : tuples){
-
if(tuple.get(0) instanceof Float){
-
count = ((Float) tuple.get( 0)).intValue();
-
} else if(tuple.get(0) instanceof Integer){
-
count = (Integer) tuple.get( 0);
-
} else if(tuple.get(0) instanceof Double){
-
count = ((Double) tuple.get( 0)).intValue();
-
}
-
}
-
return count;
-
}