Sobes.tech
Junior — Middle

What is the role of the @Dao annotation in the Room library for Android?

sobes.tech AI

Answer from AI

The @Dao annotation in the Room library for Android indicates an interface or abstract class that contains methods for accessing the database.

Role of @Dao:

  • Defines a set of database operations (insert, update, delete, queries).
  • Room automatically generates the implementation of these methods at compile time.
  • Allows separation of data access logic from the rest of the application code.

Example:

@Dao
interface UserDao {
    @Query("SELECT * FROM users WHERE id = :userId")
    fun getUserById(userId: Int): User

    @Insert
    fun insertUser(user: User)
}

Thus, @Dao serves to define data access contracts, and Room generates the necessary code to work with SQLite.