惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
Webroot Blog
Webroot Blog
T
Troy Hunt's Blog
S
Secure Thoughts
S
Security @ Cisco Blogs
S
Security Affairs
Forbes - Security
Forbes - Security
W
WeLiveSecurity
H
Hacker News: Front Page
T
Threatpost
Google Online Security Blog
Google Online Security Blog
S
Schneier on Security
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - Franky
腾讯CDC
IT之家
IT之家
博客园 - 聂微东
L
LINUX DO - 最新话题
罗磊的独立博客
Hacker News - Newest:
Hacker News - Newest: "LLM"
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 三生石上(FineUI控件)
Hacker News: Ask HN
Hacker News: Ask HN
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
C
CERT Recently Published Vulnerability Notes
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cisco Talos Blog
Cisco Talos Blog
S
SegmentFault 最新的问题
酷 壳 – CoolShell
酷 壳 – CoolShell
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
美团技术团队
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
AI
AI
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Jina AI
Jina AI
Help Net Security
Help Net Security
N
News | PayPal Newsroom
月光博客
月光博客
Spread Privacy
Spread Privacy
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
N
News and Events Feed by Topic

博客园 - 轻舟软件

轻舟分公司协作平台:统一管理、高效协作 [ERR] 1118 - Row size too large (> 8126) IDEA中Java代码修改后及时生效的方法 python SQLite 访问组件 AI课堂笔记:AI编程 轻舟项目管理系统:台账・文件・协作・管控,让项目告别混乱,有序可控! 分公司项目的柔性管理(造价咨询、招标代理) 咨询行业——项目费用管控 咨询行业——项目管理模型 Java8:函数式接口、Lambda、Stream Java知识图谱(转) 简约至上 软件需求分析方法论(转) EasyUI的属性、事件、方法的使用 两位小数偶感 什么是函数? MySql 、Oracle 获取表结构和字段信息 马云卸任演讲全文(2019-09-10) MySQL权限管理常用命令
HQL分页查询、分组查询
轻舟软件 · 2020-03-19 · via 博客园 - 轻舟软件

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