Interface UserRepository
- All Superinterfaces:
org.springframework.data.repository.CrudRepository<User,,String> org.springframework.data.repository.ListCrudRepository<User,,String> org.springframework.data.repository.ListPagingAndSortingRepository<User,,String> org.springframework.data.mongodb.repository.MongoRepository<User,,String> org.springframework.data.repository.PagingAndSortingRepository<User,,String> org.springframework.data.repository.query.QueryByExampleExecutor<User>,org.springframework.data.repository.Repository<User,String>
@Repository
public interface UserRepository
extends org.springframework.data.mongodb.repository.MongoRepository<User,String>
Repository interface for accessing and manipulating User documents in MongoDB.
This repository provides methods for CRUD operations on User entities, as well as custom queries for user authentication and management. It leverages Spring Data MongoDB's query method naming conventions for defining custom finder methods.
As a Spring Data repository, it automatically provides standard operations like:
- Saving and updating user accounts
- Deleting user accounts
- Finding user accounts by ID
- Retrieving all user accounts
The interface also defines custom queries specifically for user authentication, email verification, and username verification. These methods are essential for the security and user management aspects of the application.
- Since:
- 2025-05-15
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoiddeleteByUsername(String username) Deletes a user by username.existsByEmail(String email) Checks if an email address already exists in the database.existsByUsername(String username) Checks if a username already exists in the database.findByEmail(String email) Finds a user by email address.findByUsername(String username) Finds a user by username.Methods inherited from interface org.springframework.data.repository.CrudRepository
count, delete, deleteAll, deleteAll, deleteAllById, deleteById, existsById, findById, saveMethods inherited from interface org.springframework.data.repository.ListCrudRepository
findAll, findAllById, saveAllMethods inherited from interface org.springframework.data.repository.ListPagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.mongodb.repository.MongoRepository
findAll, findAll, insert, insertMethods inherited from interface org.springframework.data.repository.PagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.query.QueryByExampleExecutor
count, exists, findAll, findBy, findOne
-
Method Details
-
findByUsername
Finds a user by username. This method is primarily used for authentication and login.- Parameters:
username- the username to search for- Returns:
- an Optional containing the user if found, or empty if not
-
findByEmail
Finds a user by email address. This method can be used for password reset functionality or email verification.- Parameters:
email- the email address to search for- Returns:
- an Optional containing the user if found, or empty if not
-
existsByUsername
Checks if a username already exists in the database. This is used during registration to ensure username uniqueness.- Parameters:
username- the username to check- Returns:
- true if the username exists, false otherwise
-
existsByEmail
Checks if an email address already exists in the database. This is used during registration to ensure email uniqueness.- Parameters:
email- the email address to check- Returns:
- true if the email exists, false otherwise
-
deleteByUsername
Deletes a user by username. This method provides an alternative way to delete users when the ID is not available.- Parameters:
username- the username of the user to delete
-