我有一堂很基本的课:

@Entity
@Table(name = "ApplicationType")
public class ApplicationType {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long applicationTypeId;
    private String description;
    @OneToMany(mappedBy = "applicationType")
    private Set<Application> applications;

   // getters setters
  }


和一个控制器:

@GetMapping("/application/types")
public ResponseEntity<?> getApplicationTypes() {
    List<ApplicationType> applicationTypes = applicationTypeRepository.findAll();
    return new ResponseEntity<>(applicationTypes, HttpStatus.OK);
}


我的数据库有findAll();,50000 of applicationType 150000 of applicationType 2返回的100000个应用程序

如何轻松提取每种类型有多少个应用程序而不会导致API崩溃?我的解决方案是循环所有应用程序并检查它们的类型,但这非常糟糕

最佳答案

您可以使用Spring Data(> 1.7.1),并在您的CrudRepository<Application, "Primary ID Type of Application Table">中使用类似以下方法:

 Long countByApplicationType(String applicationType);

07-24 09:33