HQL分頁查詢、分組查詢


1、環境

Spring Data JPA、Spring Boot

2、利用HQL實現動態條件、分頁查詢。

(1)DAO中的定義

    /**
     *  分頁查詢預警信息(注:三個對象並無建立對象引用關系,即對應表之間並沒有建立外鍵關聯,只是存儲了ID值)
     * @param yjfl 預警分類
     * @param yjjb 預警級別
     * @param bq 標簽
     * @param beginDate 預警時間(開始時間)
     * @param endDate 預警時間(結束時間)
     * @param xm 人員姓名
     * @param gmsfzhm 公民身份證號碼
     * @param pid 人員ID
     * @param pageable 分頁信息
     * @return
     */
    @Query(
            value = "select t,p from WarningInfo t, BigRelation b, Person p"
                + " where t.id = b.ktId"
                + " and b.ztId = p.id"
                + " and (t.yjfl = ?1 or ?1 is null)"
                + " and (t.yjjb = ?2 or ?2 is null)"
                + " and (t.bq like ?3 or ?3 is null)"
                + " and (t.sj >= ?4 or ?4 is null)"
                + " and (t.sj <= ?5 or ?5 is null)"
                + " and b.gx = 'GX_R_YJXX_ZT'"
                + " and (p.xm like ?6 or ?6 is null)"
                + " and (p.gmsfzhm = ?7 or ?7 is null)"
                + " and (p.id = ?8 or ?8 is null)"
                + " order by ?#{#pageable}", 

                  countQuery = 
                      "select count(*) from WarningInfo t, BigRelation b, Person p"
                    + " where t.id = b.ktId"
                    + " and b.ztId = p.id"
                    + " and (t.yjfl = ?1 or ?1 is null)"
                    + " and (t.yjjb = ?2 or ?2 is null)"
                    + " and (t.bq like ?3 or ?3 is null)"
                    + " and (t.sj >= ?4 or ?4 is null)"
                    + " and (t.sj <= ?5 or ?5 is null)"
                    + " and b.gx = 'GX_R_YJXX_ZT'"
                    + " and (p.xm like ?6 or ?6 is null)"
                    + " and (p.gmsfzhm = ?7 or ?7 is null)"
                    + " and (p.id = ?8 or ?8 is null)"
                    + " order by ?#{#pageable}"
                    
              )
    Page<Object[]> findWarningInfo(String yjfl, String yjjb, String bq, Date beginDate, 
    Date endDate, String xm, String gmsfzhm, Long pid, Pageable pageable);

(2)調用示例

    public Result queryYj(final WarningInfoVo vo)
    {
        Sort sort = new Sort("desc".equals(vo.getOrder())?Direction.DESC:Direction.ASC, vo.getSort()); // 排序
        Pageable pageable = new PageRequest(vo.getPage() - 1, vo.getRows(), sort); // 分頁查詢條件
        
        String yjfl = vo.getYjfl();    //預警分類
        String yjjb = vo.getYjjb();    //預警級別
        String bq = vo.getBq();    //標簽
        String xm = vo.getXm();    //人員姓名
        String gmsfzhm = vo.getGmsfzhm();    //公民身份證號碼
        String sj_start = vo.getSj_start();    //開始時間
        String sj_end = vo.getSj_end();    //結束時間
                
        //條件加工
        if(!xx.isEmpty(bq))
        {
            bq = "%," + bq + ",%";
        }

        if(!xx.isEmpty(xm))
        {
            xm = "%" + xm + "%";
        }
        
        Date beginDate = null;
        if(!xx.isEmpty(sj_start))
        {
            beginDate = xx.toDate(sj_start);
        }
        
        Date endDate = null;
        if(!xx.isEmpty(sj_end))
        {
            endDate = xx.toTime(sj_end + " 23:59:59");
        }
        
        // 查詢        
        Page<Object[]> page = dao.findWarningInfo(yjfl, yjjb, bq, beginDate, endDate, xm, gmsfzhm, null, pageable);

        //轉換構建列表數據
        List<Object[]> arrList = page.getContent();
        List<WarningInfoDto> list = new ArrayList<>(arrList.size());
        for(Object[] arr: arrList)
        {
            list.add(new WarningInfoDto((WarningInfo)arr[0], (Person)arr[1]));
        }
        
        //構建返回結果
        Result result =new Result();
        result.setRows(list);
        result.setTotal(page.getTotalElements());        
        return result;        
    }    

3、分組查詢

(1)DAO中的定義

    @Query("select count(*) as num, t.hy as hy from  DataResource t where t.state.code='06'group by t.hy order by t.hy.orderNum ")
    List<Object> findGroupByHy();
    
    @Query("select count(*) as num, t.yw as yw from  DataResource t where t.state.code='06' group by t.yw order by t.yw.orderNum")
    List<Object> findGroupByYw();

(2)調用示例

  public List<StatisticsVo> statisticsThisLv1Group(boolean isYw) 
  { List
<StatisticsVo> list = new ArrayList<>(); if (isYw) {//業務 List<Object> _list = dao.findGroupByYw(); for(Object row:_list) { Object[] cells = (Object[]) row; Long num = (Long) cells[0]; CodeBusinessType sort = (CodeBusinessType) cells[1]; System.out.println(sort.getName()+" "+num); StatisticsVo vo = new StatisticsVo(); vo.setCount(num); vo.setCode(sort.getId() + ""); vo.setName(sort.getName()); list.add(vo); } } else {//行業 List<Object> _list = dao.findGroupByHy(); for(Object row:_list) { Object[] cells = (Object[]) row; Long num = (Long) cells[0]; CodeIndustry sort = (CodeIndustry) cells[1]; System.out.println(sort.getName()+" "+num); StatisticsVo vo = new StatisticsVo(); vo.setCount(num); vo.setCode(sort.getId() + ""); vo.setName(sort.getName()); list.add(vo); } } return list; }

 


免責聲明!

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



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