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:
  • User
  • MongoRepository
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Deletes a user by username.
    Checks if an email address already exists in the database.
    Checks if a username already exists in the database.
    Finds a user by email address.
    Finds a user by username.

    Methods inherited from interface org.springframework.data.repository.CrudRepository

    count, delete, deleteAll, deleteAll, deleteAllById, deleteById, existsById, findById, save

    Methods inherited from interface org.springframework.data.repository.ListCrudRepository

    findAll, findAllById, saveAll

    Methods inherited from interface org.springframework.data.repository.ListPagingAndSortingRepository

    findAll

    Methods inherited from interface org.springframework.data.mongodb.repository.MongoRepository

    findAll, findAll, insert, insert

    Methods inherited from interface org.springframework.data.repository.PagingAndSortingRepository

    findAll

    Methods inherited from interface org.springframework.data.repository.query.QueryByExampleExecutor

    count, exists, findAll, findBy, findOne
  • Method Details

    • findByUsername

      Optional<User> findByUsername(String username)
      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

      Optional<User> findByEmail(String email)
      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

      Boolean existsByUsername(String username)
      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

      Boolean existsByEmail(String email)
      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

      void deleteByUsername(String username)
      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