zoukankan      html  css  js  c++  java
  • 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; }
  • 相关阅读:
    redhat 6.4下PXE+Kickstart无人值守安装操作系统
    ubuntu14.04安装好Hadoo之后接着安装hbase和介绍常用命令
    避坑之Hadoop安装伪分布式(Hadoop3.2.0/Ubuntu14.04 64位)
    kindeditor文件上传设置文件说明为上传文件名(JSP版)
    sqlmap 扫描注入漏洞
    局域网内访问不同网段的主机(转记)
    cmd创建用户开启3389命令
    用python来更改windows开机密码
    代码安全之上传文件
    web渗透(转)
  • 原文地址:https://www.cnblogs.com/rulian/p/12527316.html
Copyright © 2011-2022 走看看