-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
iOrchid
committed
Mar 24, 2024
1 parent
ed58d3f
commit 58873ba
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
jetpack/src/main/java/org/zhiwei/jetpack/components/room/Student.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
36 changes: 36 additions & 0 deletions
36
jetpack/src/main/java/org/zhiwei/jetpack/components/room/StudentDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} | ||
|
||
|
||
} |
9 changes: 9 additions & 0 deletions
9
jetpack/src/main/java/org/zhiwei/jetpack/components/room/StudentRepo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.zhiwei.jetpack.components.room | ||
|
||
/** | ||
* 定义数据层的管理类,上接viewModel,下承数据库或网络 | ||
*/ | ||
class StudentRepo { | ||
|
||
|
||
} |