Spring Boot Jpa 简明教程

Spring Boot JPA - Repository methods

现在让我们分析在已创建的存储库接口中可用的方法。

Let’s now analyze the methods available in repository interface which we’ve created.

Repository - EmployeeRepository.java

以下为实现上述实体创建CRUD操作的存储库的默认代码,Employee。

Following is the default code of Repository to implement CRUD operations on above entity, Employee.

package com.tutorialspoint.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.tutorialspoint.entity.Employee;

@Repository
public interface EmployeeRepository extends CrudRepository<Employee, Integer>  {
}

现在,此存储库默认包含以下方法。

Now this repository contains following methods by default.

Sr.No

Method & Description

1

count(): long returns the number of entities available.

2

delete(Employee entity): void deletes an entity.

3

deleteAll():void deletes all the entities.

4

deleteAll(Iterable< extends Employee > entities):void deletes the entities passed as argument.

5

deleteAll(Iterable< extends Integer > ids):void deletes the entities identified using their ids passed as argument.

6

existsById(Integer id):boolean checks if an entity exists using its id.

7

findAll():Iterable< Employee > returns all the entities.

8

findAllByIds(Iterable< Integer > ids):Iterable< Employee > returns all the entities identified using ids passed as argument.

9

findById(Integer id):Optional< Employee > returns an entity identified using id.

10

save(Employee entity): Employee saves an entity and return the updated one.

11

saveAll(Iterable< Employee> entities): Iterable< Employee> saves all entities passed and return the updated entities.