【问题标题】:How to apply a criteria builder together with an arraylist in JPA?如何在 JPA 中将标准构建器与数组列表一起应用?
【发布时间】:2021-10-23 08:31:53
【问题描述】:

我试图在我的 api 中接收一个包含城市名称的数组,并创建一个带有返回该城市列表的标准 bulder 的过滤器。

这就是我的控制器中的内容

@GetMapping("/weather")
    public @ResponseBody List<Weather> weatherGetALL(
            @RequestParam(required = false) String order,
            @RequestParam(required = false) List<String>  city,
            @DateTimeFormat(pattern = DATE_PATTERN) Date date) {
            return this.weatherService.findAll(order, city, date);
    }

这就是我在服务中应用这些类型的过滤器的原因

@Autowired
WeatherRepository _weatherRepo;

public Weather save(Weather weather) {
    return this._weatherRepo.save(weather);
}

public Weather findeById(Integer id) {
    return this._weatherRepo.findById(id).orElse(null);
}


public List<Weather> findAll(String order, List<String>  city, Date date  ) {

    List<Weather> weather  = this._weatherRepo.findAll((Specification<Weather>)(root, cq, cb) -> {
        Predicate p = cb.conjunction();

        if(!(city == null)){
            System.out.println("entra en el arreglo");  

//          for (String name : city) {
//              p = cb.and(p, cb.equal(root.get("city"), name));
//          }


        }

        /*if (!StringUtils.isEmpty(city)) {
        System.out.println("ciudad:" + name);
            p = cb.and(p, cb.equal(root.get("city"), city ));
        }*/

        if (Objects.nonNull(date)) {
            p = cb.and(p, cb.equal(root.get("date"), date ));
        }


        if (!StringUtils.isEmpty(order)) {
            if (!StringUtils.isEmpty(order)) {
                switch(order) {
                    case "date":
                        cq.orderBy(cb.asc(root.get("date")));
                        break;
                    case "-date":
                        cq.orderBy(cb.desc(root.get("date")));
                        break;
                    default:
                        cq.orderBy(cb.asc(root.get("id")));
                }
            }
        }



        return p;
    });

    return weather;

}

我用“in”操作符读到了,我可以做到,但我还没有找到正确的方法来应用它。显然我收到的一切都很好,唯一让我失望的是那部分的过滤

【问题讨论】:

    标签: java spring-boot spring-data-jpa criteria-api criteriaquery


    【解决方案1】:

    试试这个

    public List<Weather> findAll(String order, List<String> city, Date date) {
       List<Weather> weather  = this._weatherRepo.findAll((Specification<Weather>) (root, cq, cb) -> {
                final List<Predicate> predicates = new ArrayList<>();
    
                if (date != null) {
                    predicates.add(cb.equal(root.get("date"), date));
                }
    
                if (city != null && !city.isEmpty()) {
                    predicates.add(root.get("city").in(city));
                }
    
                if (order != null && order.equals("-date")) {
                    cq.orderBy(cb.desc(root.get("date")));
                } else {
                    cq.orderBy(cb.asc(root.get("date")));
                }
    
    //            return cb.or(predicates.toArray(new Predicate[]{}));
                return cb.and(predicates.toArray(new Predicate[]{}));
            });
      
       return weather;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-17
      • 1970-01-01
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多