Java 有限JPA数据检索

Java 有限JPA数据检索,java,spring,jpa,Java,Spring,Jpa,这里是从这个项目的简化摘录CarController类应该返回来自JPA存储库的汽车集合。我正在将参数传递给将处理DB数据提取的CarServiceclassretrieveAllCars()方法 @RestController public class CarController { private CarService = carService; @GetMapping("/cars") public ResponseEntity getCars

这里是从这个项目的简化摘录
CarController
类应该返回来自JPA存储库的汽车集合。我正在将参数传递给将处理DB数据提取的
CarService
class
retrieveAllCars()
方法

@RestController
public class CarController {
    private CarService = carService;

    @GetMapping("/cars")
    public ResponseEntity getCars(@Param("brandName") Optional<String> brandName,
                                  @Param("type") Optional<String> type){
    return ResponseEntity.ok(carService.retrieveAllCars(brandName, type)); 
    }
}

@Service
public class CarService {
    private CarRepository carRepository;

    public List<Car> retrieveAllCars(String brandName, String type){
        List<Car> carList = new ArrayList<>(); 
          
        carList = carRepository.findall();
        return carList;
    }
}
@RestController
公共类汽车控制器{
私家车服务=私家车服务;
@GetMapping(“/cars”)
公共响应getCars(@Param(“品牌名称”)可选品牌名称,
@参数(“类型”)可选类型){
返回ResponseEntity.ok(carService.retrieveAllCars(brandName,type));
}
}
@服务
公营汽车服务{
私人转托管转托管;
公共列表检索所有汽车(字符串品牌名称,字符串类型){
List carList=new ArrayList();
carList=carRepository.findall();
返回卡利斯特;
}
}

当前
findall()
应查找所有车辆。现在,我只想使用方法参数
brandName
type
查找具有特定属性的汽车。实现这一目标的最佳方法是什么?有没有办法将这些参数传递给JPA方法以检索有限的数据?

只需将FindBrandNameandType(String brandName,String type)方法添加到CarRepository中即可。

您考虑过使用本机查询吗?是的,类似于
列表carList=retrieveLimitedCars(brandName,type)并在存储库界面中创建本机查询。我不知道怎么做,这只是我的第二个选择。我更喜欢使用JPA方法来检索有限的数据。