如何使用最新的 spring-boot 在 jpa 测试中使用 where 子句计算记录?

How to count records with where clause in jpa test with latest spring-boot?

我有 UserRepository:

public interface UserRepository extends JpaRepository<User, String> {}

实体:

@Entity
@Table(schema="test", name = "TBL_USERS")
@Builder
@AllArgsConstructor
public class User implements Persistable<String>  {

    @Id
    @Column(name = "ID", columnDefinition = "char")
    private String id;

    @NotNull
    @Column(name = "NAME", columnDefinition = "char", nullable = false)
    private String name;
    ...
}

在我的测试中,我想计算具有特定名称的记录,如查询:

select count(*) from TBL_USERS where name='John';

@Test
    public void testCountSimilarNames() {
        ...
        userRepository.count() ... ?
    }

我使用最新的spring-boot。

你需要这样的东西:

public interface UserRepository extends CrudRepository<User , String >{
    Integer countByName(String name);
}