JPA分組查詢,求和,自定義查詢字段,自定義VO承接


 

一.JPA使用過程中的問題

    JPA常用的查詢方法,用實體對應的repository的執行find方法,查詢都是實體的全部字段或者其中的單個字段。

如果對一個字段進行分組查詢時,會出現問題,這里分享一個自定義查詢方法。

二.解決問題

    一.分組查詢

        表數據

         

        JPA對應實體

        

        結果集VO

        

 進行分組查詢

  1. @RestController
  2. @RequestMapping("/jpa")
  3. @Api(value="/test", tags="JPA分組")
  4. public class JPAController {
  5.  
  6. @Autowired
  7. private EntityManager entityManager;
  8.  
  9.  
  10. @ApiOperation(value="JPA分組", notes = "JPA分組測試")//swagger文檔注解
  11. @GetMapping("student")
  12. public List<StudentVO> jpa(){
  13. CriteriaBuilder cb = entityManager.getCriteriaBuilder();
  14. CriteriaQuery<Tuple> cq = cb.createTupleQuery();
  15. Root<Student> root = cq.from(Student.class); //具體實體的Root
  16. root.alias( "o");//別名
  17. cq.multiselect(root.get( "name").alias("name"),root.get("age").alias("age"));
  18.  
  19. Predicate where = cb.conjunction();
  20. //增加查詢條件
  21. cq.where(where).groupBy(root.get( "name"),root.get("age"));
  22. List<Tuple> tuples = entityManager.createQuery(cq).getResultList();
  23. //結果集
  24. List<StudentVO> result = new ArrayList<>();
  25. for(Tuple tuple : tuples){
  26. result.add( new StudentVO(tuple.get(0).toString(),tuple.get(1).toString()));
  27. }
  28. return result;
  29. }
  30. }

結果集截圖(swagger文檔接口調試)

 

根據自己的情況,寫對應的JPA過濾代碼。

 

 

業務場景:

  1. /**
  2. * 查詢月份集合(JPA分組)
  3. * @param type
  4. * @return
  5. */
  6. public Integer countNum(MonthlyParamVO param,String type,String isF ,Collection<Integer> functionSonList,Collection<Integer> orgIds){
  7. //初始化結果集
  8. List<String> monthList = new ArrayList<String>();
  9. //初始化JPA查詢
  10. CriteriaBuilder cb = entityManager.getCriteriaBuilder();
  11. CriteriaQuery<Tuple> cq = cb.createTupleQuery();
  12. Root<AttendanceMonthlyReportEntity> root = cq.from(AttendanceMonthlyReportEntity.class);
  13. //加條件
  14. if("missDays".equals(type)){
  15. cq.multiselect(cb.sum(root.get( "missDays")));
  16. } else if("absentDays".equals(type)){
  17. cq.multiselect(cb.sum(root.get( "absentDays")));
  18. } else if("lateEarly".equals(type)){
  19. cq.multiselect(cb.sum(root.get( "lateEarly")));
  20. } else if("notPunched".equals(type)){
  21. cq.multiselect(cb.sum(root.get( "notPunched")));
  22. }
  23. //查詢條件where
  24. List<Predicate> predicates = new ArrayList<>();
  25. if(StringUtils.isNotBlank(param.getZhName())){
  26. predicates.add(cb.equal(root.get( "zhName"),param.getZhName()));
  27. }
  28. if(null!=param.getOrgIds()&&param.getOrgIds().size()!=0){
  29. predicates.add(root.get( "orgId").in(orgIds));
  30. }
  31. if(StringUtils.isNotBlank(param.getYear())&&StringUtils.isBlank(param.getMonth())){
  32. predicates.add(cb.equal(root.get( "attendanceMonth"),param.getYear()+"%"));
  33. }
  34. if(StringUtils.isNotBlank(param.getZhName())&&StringUtils.isNotBlank(param.getMonth())){
  35. predicates.add(cb.equal(root.get( "attendanceMonth"),param.getYear()+"-"+param.getMonth()));
  36. }
  37. if("Y".equals(isF)){
  38. //加入需求限制
  39. predicates.add(cb.or(root.get( "orgId").in(functionSonList),cb.like(root.get("positionLevel"),"F")));
  40. }
  41. cq.where(predicates.toArray( new Predicate[predicates.size()]));
  42.  
  43. List<Tuple> tuples = entityManager.createQuery(cq).getResultList();
  44. //求和
  45. Integer count = 0;
  46. for(Tuple tuple : tuples){
  47. if(tuple.get(0) instanceof Float){
  48. count = ((Float) tuple.get( 0)).intValue();
  49. } else if(tuple.get(0) instanceof Integer){
  50. count = (Integer) tuple.get( 0);
  51. } else if(tuple.get(0) instanceof Double){
  52. count = ((Double) tuple.get( 0)).intValue();
  53. }
  54. }
  55. return count;
  56. }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM