Explain few CrudRepository methods.

CrudRepository provides a small, ready-made set of methods that cover the standard create, read, update, and delete operations on an entity, so implementers don't need to hand-write basic persistence code.

Key Points: • save(entity) inserts a new entity or updates an existing one. • findById(id) returns an Optional<T> containing the entity if found. • findAll() retrieves every entity of that type from the table. • deleteById(id) removes the entity matching the given identifier. • count() returns the total number of entities, and existsById(id) checks for existence without loading the full entity.

Example: A simple UserRepository extending CrudRepository<User, Long> immediately gains the ability to save a new user, look one up by ID, list all users, and delete a user, all without writing any SQL or implementation code.

Code Example:

public interface UserRepository extends CrudRepository<User, Long> {
}

// Usage
User saved = userRepository.save(new User("Alice"));
Optional<User> found = userRepository.findById(saved.getId());
Iterable<User> all = userRepository.findAll();
userRepository.deleteById(saved.getId());
long total = userRepository.count();

Interview Tip: A concise interview answer is:

"CrudRepository gives me save() for insert/update, findById() returning an Optional, findAll() to list everything, deleteById() to remove a record, and count()/existsById() for quick checks -- all without writing any implementation, just by extending the interface."