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