sql - 在 spring 数据 jpa 存储库中通过许多可选参数搜索

标签 sql spring spring-boot spring-data-jpa

所以我有表格评论和作者。我想用许多可选参数构建复杂的搜索栏。我想使用可选的作者名字/姓氏和一些诸如流行度(基于评论评级)之类的标志来过滤评论。

由于我不知道如何使用 spring 数据 jpa 存储库编写它,我一直在考虑将其编写为带有 @Query 注释的 native 查询,这样应该可以工作

Select c.* from comment c join author a on a.id = c.author_id
Where (:firstname = '' or (:firstname = a.firstname)) 
And (:lastname = '' or (:lastname = a.lastname))
And (:popular = false or (c.rating > 25))

是否可以选择使用 spring 数据 jpa 编写它?

例如,将来我计划添加更多参数和分页。使用 spring 就像 1 分钟的 sql 查询,我将失去几个小时。

在这种情况下是否有一些最佳实践?

最佳答案

我建议使用 JpaSpecificationExecutor存储库方法 findAll(Specification<T> spec, Pageable pageable) .此解决方案允许您使用相同的存储库和服务 API 扩展参数列表
实体:

@Entity
@Table(name = "author")
public class Author {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private Long id;

    @Column(name = "firstname")
    String firstname ;

    @Column(name = "lastname")
    String lastname ;

    // getters, setters, equals, hashcode, toString ...
}

@Entity
@Table(name = "comment")
public class Comment {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private Long id;

    @ManyToOne
    @JoinColumn(name = "author_id")
    Author author;

    @Column(name = "rating")
    Integer rating;

    // getters, setters, equals, hashcode, toString ...
}
存储库:
@Repository
public interface CommentRepository 
    extends JpaRepository<Comment, Long>, JpaSpecificationExecutor<Comment> {

}
规范: org.springframework.data.jpa.domain.Specification
public class CommentSpecs {

      /** if firstname == null then specification is ignored */
      public static Specification<Comment> authorFirstnameEquals(String firstname) {
        return (root, query, builder) -> 
             firstname == null ? 
                 builder.conjunction() :
                 builder.equal(root.get("author").get("firstname"), firstname);
      }

      /** if lastname == null then specification is ignored */
      public static Specification<Comment> authorLastnameEquals(String lastname) {
        return (root, query, builder) -> 
             lastname == null ? 
                 builder.conjunction() :
                 builder.equal(root.get("author").get("lastname"), lastname);
      }

      /** if rating == null then specification is ignored */
      public static Specification<Comment> ratingGreaterThan(Integer rating) {
        return (root, query, builder) -> 
             rating == null ? 
                 builder.conjunction() :
                 builder.greaterThan(root.get("rating"), rating);
      }
}
服务方法参数:
public class CommentParameters {

  String authorFirstname;
  String authorLastname;
  Integer rating;

  // getters, setters

}
所有参数都可以为空。您可以只设置您需要的参数。如果参数为空,我们的规范会忽略它
服务:
@Service
public class CommentService {

  @Autowired
  CommentRepository repository;
                
  public List<Comment> getComments(CommentParameters params, Pageable pageable) {

      Specification spec1 = CommentSpecs.authorFirstnameEquals(params.getAuthorFirstname());                                            
      Specification spec2 = CommentSpecs.authorLastnameEquals(params.getAuthorLastname());
      Specification spec3 = CommentSpecs.ratingGreaterThan(params.getRating());

      Specification spec = Specifications.where(spec1).or(spec2).or(spec3);
                                                                    
      return repository.findAll(spec, pageable);

   }
}
我使用文本编辑器编写了代码,因此需要修改。但我认为要点很容易发现

关于sql - 在 spring 数据 jpa 存储库中通过许多可选参数搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58240024/

相关文章:

mysql - 从具有特定外键的其他表获取记录

Mysql UNION ALL 创建双记录

sql - 与层次顺序和缩进空间相关的查询

java - SpringBoot Heroku org.maven.plugins :maven-compiler-plugin:3. 8.1:编译失败

mysql - 获取用户在评分表中的排名

java - 默认 ResourceBundleMessageSource 有什么意义?

java - 从 Eclipse Mars EE 在 Tomcat 7 和 8 上部署应用程序时出现问题

java - 构建spring框架源码遇到错误

java - @ExceptionHandler(Exception.class) 不处理所有类型的异常

Spring 启动应用程序-test.properties