android:room을사용하여데이터보관
room을사용하여데이터보관
차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판이전 판다음 판 | 이전 판 | ||
android:room을사용하여데이터보관 [2024/07/29 20:17] – 이거니맨 | android:room을사용하여데이터보관 [2024/12/24 20:04] (현재) – 이거니맨 | ||
---|---|---|---|
줄 5: | 줄 5: | ||
<code kotlin> | <code kotlin> | ||
//Room 버전 | //Room 버전 | ||
- | | + | |
- | // Room | + | // Room |
- | implementation " | + | implementation(" |
- | kapt " | + | implementation(" |
+ | // Kotlin annotation processingtool | ||
+ | kapt (" | ||
</ | </ | ||
+ | |||
+ | 플러그인에도 다음을 추가해야 한다. | ||
+ | |||
+ | <code kotlin> | ||
+ | plugins { | ||
+ | ... | ||
+ | id(" | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <WRAP center round tip 90%> | ||
+ | 참고로 2024. 7. 29. 현재 구글검색을 하면 나오는 gradle script 예제들은 Groovy 언어에 기반한 것이다((https:// | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== 뼈대 만들기 ===== | ||
+ | |||
+ | ==== 1. 데이터 클래스 생성 ==== | ||
+ | |||
+ | 데이터 클래스를 생성하여 구조화된 자료를 만들자. | ||
+ | |||
+ | 이를테면 다음과 같이 만들 수 있을 것이다. | ||
+ | |||
+ | |||
+ | <code kotlin> | ||
+ | data class Todo( | ||
+ | var id : Int = 0, | ||
+ | var title : String, | ||
+ | var createdAt : LocalDateTime | ||
+ | ) | ||
+ | |||
+ | |||
+ | |||
+ | </ | ||
+ | [[android: | ||
+ | |||
+ | ==== 2. 엔티티 생성 ==== | ||
+ | |||
+ | 위 데이터 클래스에 애노테이션을 추가하면 엔티티를 만들 수 있다. | ||
+ | |||
+ | 데이터 클래스 위에 " | ||
+ | |||
+ | 그 외에 데이터베이스에서 다루는 개념들도 애노테이션으로 추가 가능하다. 이를테면 프라이머리키는 " | ||
+ | |||
+ | 다음은 그 예시다. | ||
+ | |||
+ | Contact.kt를 다음과 같이 만들었다. | ||
+ | |||
+ | |||
+ | <file kotlin Contact.kt> | ||
+ | package com.dklaw.gogo2.database | ||
+ | |||
+ | import androidx.room.Entity | ||
+ | import androidx.room.PrimaryKey | ||
+ | |||
+ | @Entity | ||
+ | data class Contact( | ||
+ | |||
+ | val firstName : String, | ||
+ | val lastName : String, | ||
+ | val phoneNumber : String, | ||
+ | val phoneNumber2 : String, | ||
+ | val officeNumber : String, | ||
+ | val faxNumber : String, | ||
+ | val email : String, | ||
+ | val address : String, | ||
+ | val idCardAddress : String, | ||
+ | val zipcode : String, | ||
+ | val socialNumber : String, | ||
+ | val work : String, | ||
+ | val workAddress : String, | ||
+ | val etc : String, | ||
+ | val photo : String, | ||
+ | |||
+ | @PrimaryKey(autoGenerate = true) | ||
+ | val id : Int = 0 | ||
+ | ) | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== 2. DAO 생성 ==== | ||
+ | |||
+ | 다음과 같이 ContactDAO.kt | ||
+ | |||
+ | <file kotlin ContactDAO.kt> | ||
+ | package com.dklaw.gogo2.database | ||
+ | |||
+ | import androidx.room.Dao | ||
+ | import androidx.room.Delete | ||
+ | import androidx.room.Query | ||
+ | import androidx.room.Upsert | ||
+ | import kotlinx.coroutines.flow.Flow | ||
+ | |||
+ | @Dao | ||
+ | interface ContactDAO { | ||
+ | |||
+ | @Upsert | ||
+ | suspend fun upsertContact(contact : Contact) | ||
+ | |||
+ | @Delete | ||
+ | suspend fun deleteContact(contact: | ||
+ | |||
+ | @Query(" | ||
+ | fun getContactsOrderByFirstName() : Flow< | ||
+ | |||
+ | @Query(" | ||
+ | fun getContactsOrderByLastName() : Flow< | ||
+ | |||
+ | @Query(" | ||
+ | fun getContactsOrderByPhoneNumber() : Flow< | ||
+ | } | ||
+ | </ |
android/room을사용하여데이터보관.1722251847.txt.gz · 마지막으로 수정됨: 2024/07/29 20:17 저자 이거니맨