有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如果@RequestParameter被给定,则根据它的值进行过滤,如果没有,则什么也不做

我正在尝试使用@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());
}

我可以在controller中设置@RequestParam (defaultValue = "something"),但这是有问题的,因为我不知道“model”字段的默认值是什么,因为每辆车都有不同的车型,而且我仍然必须按默认值筛选项目,如果没有给出,我不想做任何事情

我还试图将Optional<>作为参数传递,然后在filter函数中使用if语句和ifPresent()方法检查每个参数,但我不知道如何将其组合在一起


共 (1) 个答案

  1. # 1 楼答案

    您可以这样创建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);
    

    然后从CarService调用它:

    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