当前位置: 首页 > 知识库问答 >
问题:

如果@RequestParameter已给定,则根据其值进行过滤,如果未指定,则不执行任何操作[duplicate]

充培
2023-03-14

我正在尝试使用@RequestParam,如果给定了该值,它应该从数据库中通过该参数找到的所有项目中进行过滤,如果没有,它应该什么都不做。我还想问一下函数式编程在这里是否有用。

这是我的汽车课:

import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;

import javax.persistence.*;

@Data
@Entity
@Table(name = "Cars")
public class Car {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Setter(AccessLevel.NONE)
    private Long Id;

    private int yearOfProduction;
    private int price;
    private String color;
    private String brand;
    private String model;

    @ManyToOne
    @JoinColumn(name = "customer")
    private Customer customer;

    public Car() {
    }

    public Car(int yearOfProduction, int price, String color, String brand, String model) {
        this.yearOfProduction = yearOfProduction;
        this.price = price;
        this.color = color;
        this.brand = brand;
        this.model = model;
    }
}

这是控制器,我在其中设置了要询问的参数:

@GetMapping
public List<Car> getCars(@RequestParam(required = false) Integer minPrice,
                         @RequestParam(required = false) Integer maxPrice,
                         @RequestParam(required = false) String model){

    return carService.getCars(minPrice, maxPrice, model);
}

这是汽车服务,我想做的是:

public List<Car> getCars(Integer minPrice, Integer maxPrice, String model) {
    return carRepository
            .findAll()
            .stream()
            .filter(car ->
                      //if minPrice exists
                                car.getPrice() >= minPrice
                                 &&
                       //if maxPrice exists
                                 car.getPrice() <= maxPrice
                                 &&
                       //if model exists
                                 car.getModel().equals(model))
                      .collect(Collectors.toList());
}

我可以在控制器中设置@Request estParam(defaultValue="某事"),但这是有问题的,因为我不知道“模型”字段的默认值是什么,因为每辆车都有不同的模型,我仍然必须按默认值过滤项目,这是不需要的,因为如果没有给出,我不想对它做任何事情。

我还试图通过可选


共有1个答案

施喜
2023-03-14

您可以这样创建一个jpa查询(在您的汽车存储库中):

@Query("select c from Car c where (?1 is null or c.price >= ?1) and (?2 is null or c.price <= ?2) and (?3 is null or c.model = ?3)")
List<Car> getCars(Integer minPrice, Integer maxPrice, String model);

然后从车载服务呼叫:

public List<Car> getCars(Integer minPrice, Integer maxPrice, String model) {
   return carRepository.getCars(minPrice, maxPrice, model);
}

避免postgresql中强制转换问题的一种方法是为参数使用默认值。假设您将0设置为最小价格和最大价格的默认值,并将模型设置为空字符串。

@Query("select c from Car c where (?1 = 0 or c.price >= ?1) and (?2 = 0 or c.price <= ?2) and (?3 = '' or c.model = ?3)")
List<Car> getCars(Integer minPrice, Integer maxPrice, String model);

在控制器中:

@RequestParam(defaultValue="") String model
@RequestParam(defaultValue="0") Integer minPrice
@RequestParam(defaultValue="0") Integer maxPrice
 类似资料:
  • 问题内容: 我有一个php脚本,我在CentOS上每10分钟通过cron执行一次此脚本。 问题是,如果cron作业将花费10分钟以上,则将启动同一cron作业的另一个实例。 我尝试了一个技巧,那就是: cron作业启动时,使用php代码创建了一个锁定文件(类似于pid文件)。 作业完成后,使用php代码删除了锁定文件。 当任何新的cron作业开始执行脚本时,我检查了锁定文件是否存在,如果存在,则中

  • 问题内容: 我有一个模板绑定,使用Angular的日期过滤器显示名为“ date”的模型属性,该属性是一个日期。 到目前为止,一切都很好。但是,目前,如果日期字段中没有值,则绑定不显示任何内容。但是,如果没有日期,我希望显示字符串“ Various”。 我可以使用二进制运算符获取基本逻辑: 但是我无法使其与日期过滤器一起使用: 如何在日期过滤器旁边使用二元运算符? 问题答案: 原来我要做的就是将表

  • 问题内容: 在MySQL以外的任何事物中,它感觉都非常简单。 基本上,我需要根据一个特定术语返回多少结果来切换我正在使用的索引类型以及其他一些条件。 效果: MySQL语句中可能吗? 编辑: 查询A: 查询B: 查询切换的原因是,根据“术语”的流行程度,我得到的结果时间截然不同。 问题答案: 编辑: 我下面说的关于要求存储过程的说法是不正确的。尝试这个: 确实,这是一个case表达式,在存储的pr

  • 下面是我的搜索响应示例,检索到4个结果。 现在,我想根据下面的标准,根据特定的“dir”字段值过滤上述搜索结果。 在且仅在以下情况下将搜索结果包含在响应中: 如果“dir”字段值等于“/shared”或“/private/hitesh” 如果“dir”字段值以“/shared/”或“/private/hitesh/”开头,则为其他 如何在ElasticSearch中实现上述功能?

  • 我有一个组件可能会或可能不会收到onClick属性。我想附加这个非必需的道具作为点击处理程序。 如果我不知道是否会给处理程序,那么最安全的方法是什么?