Spring DATA JPA 如何编写使用字段的常量值来获取数据的方法

sud*_*pta 6 spring spring-data-jpa

嗨,我正在使用 Spring Data JPA 并希望使用功能从方法名称生成查询。我在 DB 中有一个活动字段,它只有值 0 和 1。我想获取所有活动值为 1 的数据。这是一个常量值,所以我不想将此值作为方法参数传递。

请建议相同的方法是什么。

例子:

我有一个实体 EmailRef

public class EmailRef {

    /* other vareialbe */

    @Column(name="is_active") /* this is the field which value is 0 and 1 in DB*/
    private Integer active;

    /* setter getter method */       
}
Run Code Online (Sandbox Code Playgroud)

这是我要编写方法的存储库,该方法将获取 active 为 1 的所有数据;

public interface EmailRefRepositry extends JpaRepository<EmailRef, Long> {          

    @Query("select * from email_reference where is_active=1") /* this is the query I want to convert into method*/
    List<EmailRef> findByActive(); /*I want to write method like that which will fetch all data form table where active field value is 1*/
}
Run Code Online (Sandbox Code Playgroud)

我被困在常量谷请提出建议

谢谢苏丹舒

kim*_*y82 5

如果您可以将该整数更改为布尔值,您可以执行以下操作:

在您的实体中:

private Boolean                 active;
Run Code Online (Sandbox Code Playgroud)

在你的仓库中:

List<EmailRef> findByActiveIsTrue();
Run Code Online (Sandbox Code Playgroud)