- Spring Security(Third Edition)
- Mick Knutson Robert Winch Peter Mularien
- 217字
- 2025-04-04 17:54:29
Spring Data repositories
We will now add the required interfaces for Spring Data to map our required CRUD operations to our embedded database, by performing the following steps:
- We begin by adding a new interface in a new package, which will be com.packtpub.springsecurity.repository. The new file will be called CalendarUserRepository.java, as follows:
//com/packtpub/springsecurity/repository/CalendarUserRepository.java
package com.packtpub.springsecurity.repository;
import com.packtpub.springsecurity.domain.CalendarUser;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CalendarUserRepository
extends JpaRepository<CalendarUser, Integer> {
CalendarUser findByEmail(String email);
}
This will allow for standard CRUD operations such as find(), save(), and delete() on our CalendarUser objects.
- We can now continue by adding a new interface in the same repository package, which will be com.packtpub.springsecurity.repository, and the new file will be called EventRepository.java:
//com/packtpub/springsecurity/repository/EventRepository.java
package com.packtpub.springsecurity.repository;
import com.packtpub.springsecurity.domain.Event;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EventRepository extends JpaRepository<Event,
Integer> {}
This will allow for standard CRUD operations such as find(), save(), and delete() on our Event objects.
- Finally, we will be adding a new interface in the same repository package, which will be com.packtpub.springsecurity.repository, and the new file will be called RoleRepository.java. This CrudRepository interface will be used to manage the Role object for our security roles associated with a given CalendarUser:
//com/packtpub/springsecurity/repository/
package com.packtpub.springsecurity.repository;
import com.packtpub.springsecurity.domain.Event;
import org.springframework.data.jpa.repository.JpaRepository;
public interface RoleRepository extends JpaRepository<Role,
Integer> {}
This will allow for standard CRUD operations such as find(), save(), and delete() on our Role objects.