使用 JPQL 的全局过滤器 Spring

Global Filter with JPQL Spring

我正在做一些项目,他们要求做一个全局过滤器,我的问题是有没有机会做一个全局过滤器而不使用 many if 来检查用户是否添加该过滤器? 这是我的一个项目的例子: 在存储库中,我添加了这段代码:

 @Query("from students s join Class_Room c on s.classRoom.id=c.id join teachers t on t.id=c.id " +
        "where c.name= :className AND t.subject= :subject AND s.name= :name")
List<Student> studentClassProf(@Param("className") String className, @Param("subject") String subject, @Param("name") String name);

@Query("from students s join Class_Room c on s.classRoom.id=c.id join teachers t on t.id=c.id " +
        "where c.name= :className AND t.subject= :subject")
List<Student> studentClassProf(@Param("className") String className, @Param("subject") String subject);

在我的控制器中 class 我使用这个:

 @GetMapping("/globalSearch")
public ResponseEntity<String> globalSearch(String studentName, String teacherName, String className, String subject) throws MyNotFoundException {
    if (studentName != null && teacherName != null && className != null && subject != null) {
        studentRepo.studentClassProf(className, subject, studentName);
    }
    if (studentName == null && teacherName != null && className != null && subject != null) {
        studentRepo.studentClassProf(className, subject);
    }
....

那么如果不添加很多if来检查字段是否为null或者我无能为力解决而我总是要写很多if块怎么办?

你可以使用 JpaSpecificationExecutor<T>.

为了使用它,您必须从 JpaSpecificationExecutor<T>:

扩展您的存储库接口
public interface StudentRepository extends JpaRepository<Student, UUID>, 
    JpaSpecificationExecutor<Student> {}

然后你可以制作一个根据参数或过滤器生成规范的方法class。

public class StudentSpecification {
    private static Specification<Student> getStudentFilterSpecification(String studentName, String teacherName, String className, String subject) {
        return (root, query, criteriaBuilder) -> {
            Predicate predicate = criteriaBuilder.conjunction();
            // Condition student name.
            if (Objects.nonNull(studentName)) {
                predicate = criteriaBuilder.and(predicate, criteriaBuilder
                        .equal(root.get("studentName"), studentName));
            }
            // Condition teacher name.
            if (Objects.nonNull(teacherName)) {
                predicate = criteriaBuilder.and(predicate, criteriaBuilder
                        .equal(root.get("teacherName"), teacherName));
            }
            // Condition class name.
            if (Objects.nonNull(className)) {
                predicate = criteriaBuilder.and(predicate, criteriaBuilder
                        .equal(root.get("className"), className));
            }
            // Condition subject.
            if (Objects.nonNull(subject)) {
                predicate = criteriaBuilder.and(predicate, criteriaBuilder
                        .equal(root.get("subject"), subject));
            }
            return predicate;
        };
    }
}

现在您可以发送带有规格的搜索请求了。

@GetMapping("/globalSearch")
public ResponseEntity<String> globalSearch(
        String studentName, String teacherName, String className, String subject)
        throws MyNotFoundException {
    studentRepo.findAll(StudentSpecification
            .getStudentFilterSpecification(studentName, 
                    teacherName, className, subject));
}