Spring Data JPA - 查找数据库中的行数

时间:2021-12-13 01:06:18

I am a noob in Spring framework. I know there is the findAll method to query all the data in a database. Is there a way to count the number of rows in a database using the Spring framework and doesn't require to write a SQL query?

我是Spring框架中的菜鸟。我知道有findAll方法来查询数据库中的所有数据。有没有办法使用Spring框架计算数据库中的行数,并且不需要编写SQL查询?

2 个解决方案

#1


3  

There is method count() in CrudRepository. You can check oficial documentation.

CrudRepository中有方法count()。您可以查看官方文档。

#2


-1  

Use findAll method on a jpa entity and assign the result to a List. After the execution of findall, you can use collection API to get the size of the list:

在jpa实体上使用findAll方法并将结果分配给List。执行findall后,您可以使用集合API来获取列表的大小:

List<YourEntity> entities = YourEntity.findAll(); 
int size = entities.size();

#1


3  

There is method count() in CrudRepository. You can check oficial documentation.

CrudRepository中有方法count()。您可以查看官方文档。

#2


-1  

Use findAll method on a jpa entity and assign the result to a List. After the execution of findall, you can use collection API to get the size of the list:

在jpa实体上使用findAll方法并将结果分配给List。执行findall后,您可以使用集合API来获取列表的大小:

List<YourEntity> entities = YourEntity.findAll(); 
int size = entities.size();