小张帅三代 2020-08-26 15:27 采纳率: 100%
浏览 1680
已采纳

sprint data jpa 中Specification() 如何指定查询的字段而不是把整个表的所有字段都返回

            List<Map<String, Object>> res = null;
            try{
                res = parkDao.findAll(new Specification() {
                    @Override
                    public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder cb) {
                        List<Predicate> predicateList = new ArrayList<>();
                        criteriaQuery.multiselect();
                        if (!StringUtil.isEmpty(checkStatus)) {
                            predicateList.add(cb.equal(root.get("checkStatus"), checkStatus));
                        }
                        if (streetOfficeCode!=null) {
                            predicateList.add(cb.equal(root.get("streetOfficeCode"), streetOfficeCode));
                        }
                        if (communityCode!=null) {
                            predicateList.add(cb.equal(root.get("communityCode"), communityCode));
                        }
                        if (departmentCode!=null) {
                            predicateList.add(cb.equal(root.get("departmentCode"), departmentCode));
                        }
                        log.debug("predicateList--->"+predicateList);
                        Predicate[] pre = new Predicate[predicateList.size()];

                        criteriaQuery.where(predicateList.toArray(pre));

                        return cb.and(predicateList.toArray(pre));
                    }
                },new Sort(Sort.Direction.DESC,"id"));

这个代码构建了动态查询,但是查出了整个表的所有字段
我只需要其中的某些字段,该怎么办?

  • 写回答

2条回答 默认 最新

  • 关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波