[英]How to filter objects by scope spring boot jpa?
我有班級學生:
@Entity
class Student(
var name: String? = null,
var status: Boolean? = null
)
我想寫一個方法可以過濾Student active (status = true) 並且可以在調用其他函數后使用。 Ex 在 StudentService 中定義了一個函數獲取名稱:
fun getByName(name: String) : Student {
return studentRepository.getByName(name)
}
我希望返回一個列表學生有名字和活躍:
val listStudent = studentService.getByName("name_input").active()
我可以在哪里定義方法“active()”?
studentService.getByName("name_input")
返回List<Student>
,它沒有這樣的方法( active()
)。
要在數據庫端過濾記錄,您應該定義StudentRepository#getByNameAndStatus(String name, boolean status)
returns List<Student>
。
否則可以使用 Java 流
val listStudent = studentService.getByName("name_input")
.stream()
.filter(s-> s.status().equals(true))
.collect(Collectors.toList());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.