0

我正在开发Spring Boot + Spring Data JPA + Postgres + Lombok示例。在此示例中,我想获取所有学生的名字是 ASC 顺序并且其状态为Active.

我开发了下面的查询,效果很好,但我看不到在查询中也可以使用status=Active这里的方法。JpaRepository

注意:在我的例子中,status字段是EnumJava。

如果我们可以这样做,有什么办法吗?我知道我可以获取所有学生,然后使用流可以轻松过滤,但是使用 JpaRepository 查询,有什么办法吗?

List<Student> students = studentRepository.findAll(new Sort(Sort.Direction.ASC, "studentName"));
4

2 回答 2

3

在您的StudentRepository界面中,Repository / JpaRepository您可以添加这样的方法签名:

public interface StudentRepository extends ....{
  List<Student> findAllByStatusOrderByStudentNameAsc(String status);
}
于 2019-07-11T09:00:43.223 回答
1

只需将以下方法签名放在您的存储库中,并使用参数“Active”在需要的地方调用它。

List<Student> findAllByStatusOrderByStudentNameAsc(String status);
于 2019-07-11T08:59:31.220 回答