菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
288
0

HQL分页查询、分组查询

原创
05/13 14:22
阅读数 89933

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; }

 

发表评论

0/200
288 点赞
0 评论
收藏
为你推荐 换一批