[06] Room database of Kotlin project operation

(1) As long as a person does not give up on himself, the whole world will not give up on you.
(2) I am born to be of great use . (3) If I
cannot bear the suffering of learning, I must endure the suffering of life. How painful it is Deep insight.
(4) You must gain from doing difficult things . (
5) Spirit is the real blade.
(6) Defeating opponents twice, the first time in the heart.
(7) Living well means doing meaningful things.
(8) ) It’s not too late to fix the situation
.
(10) With strength, 365 days a year is the peak season for applicants. Without strength, every day is the off-season for applicants.
(11) The foundation is not strong, the earth is shaken.
(12) The original intention of blogging: grow yourself and help others. When you leave the world one day, I hope that the thoughts in the blog can help people guide the direction.
(13) It is not easy to write, if you like it or it is helpful to you, remember to like + follow or bookmark~

[06] Room database of Kotlin project operation

Article Directory

1.Room

(1) It is an abstraction of the SQlite database
(2) In order to access the SQLite database more smoothly

1.1 How to be fluent

(1) Adding, deleting, modifying and checking can be completed only by using annotations.
(2) Adding, deleting, modifying and checking code is generated. The generated code is completed during compilation.
(3) The generated code has been optimized a lot
(4) Transaction control is used

2. Case

2.1 Data table entity

/**
 * @author XiongJie
 * @version appVer
 * @Package com.gdc.kotlinproject.database
 * @file
 * @Description:
 * (1)@Entity注解在编译期生成代码
 * @date 2021-6-4 15:15
 * @since appVer
 */
@Entity
class Student() {
    /**
     * 主键唯一并自增
     */
    @PrimaryKey(autoGenerate = true)
    var id : Int = 0

    /**
     * 1.懒加载
     * 2.字段别名,如果不使用别名,默认使用属性名,加了别名,优先级更高,就使用别名的名称
     * name="Student_name"
     */
    @ColumnInfo(name = "name")
    lateinit var name:String

    @ColumnInfo(name = "age")
    var age:Int = 0

    /**
     * 1.次构造调用主构造
     * :this()
     */
    constructor(name:String,age:Int):this(){
        this.name = name
        this.age = age
    }
}

2.2 Database Access Object

/**
 * @author XiongJie
 * @version appVer
 * @Package com.gdc.kotlinproject.database
 * @file
 * @Description:
 * (1)@Dao标记数据库的访问对象,即数据库的增删改查
 * @date 2021-6-4 15:24
 * @since appVer
 */

@Dao
interface StudentDao {

    /**
     * 1.可变参数使用
     * vararg students:Student
     */
    @Insert
    fun insertStudents(vararg students:Student)

    @Update
    fun updateStudents(vararg students: Student)

    //1.删除全部
    @Query("DELETE FROM student")
    fun deleteAllStudents()

    //1.查询全部
    @Query("SELECT * FROM student ORDER BY ID DESC")
    fun queryAllStudents() : List<Student>

}

2.3 Database creation and acquisition

/**
 * 1.数据表与实体指定
 * java数组: entities = {}
 * kt数组:entities = []
 *
 * 2.数据库版本号设置
 * version数据库版本号
 *
 * 3.返回数据库操作Dao
 * Dao是对数据库的增删改查
 *
 * 4.创建数据库与获取数据库
 *
 * 5.需要成为RoomDatabase的子类
 *
 */
@Database(entities = [Student::class],version = 1)
abstract class StudentDatabase : RoomDatabase(){

    abstract fun getStudentDao() : StudentDao

    //派生(单例模式)
    companion object{
        //1.允许为空
        private var INSTANCE : StudentDatabase ? = null

        /**
         * 2.创建数据库获取数据库
         * (1)给Application使用
         */
        fun getDatabase(context : Context) : StudentDatabase ? {
            if(null == INSTANCE){
                /**
                 * 2.1::class.java是因为room底层是由java产现的,因此需要.java
                 * 如果底层是由kotlin语言编写的,就不需要这么麻烦
                 *
                 * 2.2允许在主线程中运行
                 * allowMainThreadQueries()
                 */
                INSTANCE = Room.databaseBuilder(context,StudentDatabase::class.java,"student_database.db")
                    .allowMainThreadQueries()
                    .build()

            }
            return INSTANCE
        }

        /**
         * 获取数据库
         */
        fun getDataBase() : StudentDatabase? = INSTANCE
    }

}

3. Reward and encouragement

Thank you for your careful reading, your encouragement is the inexhaustible motivation for my writing! ! !

3.1 WeChat Reward

Insert picture description here

3.2 Alipay rewards

Insert picture description here