Junior — Middle
How is the creation of an interface for the data access layer implemented in the project?
sobes.tech AI
Answer from AI
Creating an interface for the Data Access Object (DAO) layer in Java is usually implemented by defining an interface with methods for CRUD operations (create, read, update, delete). This interface separates business logic from the specific implementation of database access, which facilitates testing and maintenance.
Example of a DAO interface for the User entity:
public interface UserDao {
User findById(Long id);
List<User> findAll();
void save(User user);
void update(User user);
void delete(Long id);
}
Then, a class implementing this interface is created, where the specific logic for interacting with the database is written (for example, via JDBC, JPA, or Spring Data). This approach makes it easy to change the implementation without affecting the rest of the code.