Skip to content

Commit

Permalink
room的初步引入OK
Browse files Browse the repository at this point in the history
  • Loading branch information
iOrchid committed Mar 24, 2024
1 parent ed58d3f commit 58873ba
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
11 changes: 11 additions & 0 deletions jetpack/jetpack笔记速览.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ private val vm: JetpackViewModel by ViewModels()

##### 七、Room

>
room是jetpack中关于数据库操作的组件,[room](https://developer.android.google.cn/jetpack/androidx/releases/room?hl=zh-cn)

1. 使用三要素:`Entity``Dao``Database`
- 创建数据类,添加`@Entity`注解,内可声明主键,外键,表格字段名,忽略字段等。
- 创建接口Dao,使用`@Dao`注解,内声明对数据表的增删改查操作,分别是`@Insert/@Delete/@update/@Query`
,其中查询可使用`sqlite语句`
- 声明抽象类继承`RoomDatabase`,并添加注解`@Database`,内声明抽象函数获取对表操作的各个`Dao`
;另可声明一个创建`database`的单例对象的函数。
2. 注意:`room`支持普通数据类型,LiveData和flow的,可以挂起函数。

##### 八、Paging


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.zhiwei.jetpack.components.room

import androidx.room.ColumnInfo
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
import androidx.room.Query
import androidx.room.Update
import kotlinx.coroutines.flow.Flow

/**
* room数据库框架使用,1,创建entity类,记得@Entity注解
*/
@Entity(tableName = "tb_student")//定义数据表的名称
data class Student(
@PrimaryKey(autoGenerate = true)
val id: Int = 1,//定义数据表索引id,设置为主键,自增
@ColumnInfo("uName")//可以定义表字段名
val name: String,
val age: Int,
val sex: Int,//性别 0 女,1 男
val height: Int,//身高
val school: String,
val address: String,
)

/**
* 2,创建对表的crud操作
*/
@Dao//注解不可少,定义为interface类型
interface StudentDao {

//定义插入数据条目的函数,使用@Insert注解,标记类型,可配置冲突策略
@Insert(Student::class, OnConflictStrategy.ABORT)
fun insertStudent(student: Student): Int

@Delete(Student::class)
fun deleteStudent(student: Student): Int

@Update(Student::class)
fun updateStudent(student: Student): Int

/**
* 身高高于180的所有学生
*/
@Query("select * from tb_student where height > 180")
fun query180Student(): List<Student>

/**
* query语句内就是sqlite的基本查询语句,可使用sqlite的条件语句,限定语句等。
* 返回类型可以是普通的List或者单个数据,也可以是LiveData的,也可以是flow的
* 根据业务需要,可以定义函数为suspend的,或者不用
*/
@Query("select uName,age, sex, school from tb_student")
suspend fun queryStudent(): Flow<Student>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.zhiwei.jetpack.components.room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

/**
* 步骤3,创建database,注意使用@dataBase注解,声明所有表entity,注明版本号
*/
@Database(entities = [Student::class], version = 1)
abstract class StudentDatabase : RoomDatabase() {
/**
* 对应的操作表的dao函数,也是抽象的。
*/
abstract fun studentDao(): StudentDao


companion object {

private const val DB_NAME = "student"

@Volatile
private var instance: StudentDatabase? = null

fun createDatabase(context: Context): StudentDatabase {
return instance ?: Room.databaseBuilder(
context,
StudentDatabase::class.java,
DB_NAME
).build().also { instance = it }
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.zhiwei.jetpack.components.room

/**
* 定义数据层的管理类,上接viewModel,下承数据库或网络
*/
class StudentRepo {


}

0 comments on commit 58873ba

Please sign in to comment.