zoukankan      html  css  js  c++  java
  • 安卓学习02---room

    title: 安卓学习02---room
    date: 2020-02-02 18:20:13
    tags:	
    

    room是jetpack的组件,可以使程序流畅的访问sqlite。

    <!--more -->

    1、依赖的添加

    dependencies {
          def room_version = "2.2.2"
    ​
          implementation "androidx.room:room-runtime:$room_version"
          annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
    ​
          // Test helpers
          testImplementation "androidx.room:room-testing:$room_version"
        }

    2、room的使用

    1、Entity(表结构)

    相当于java web中的实体类。以单词为例,Entity应为:

    package com.example.roombasic;
    ​
    import androidx.room.ColumnInfo;
    import androidx.room.Entity;
    import androidx.room.PrimaryKey;
    ​
    @Entity
    public class Word {
        @PrimaryKey(autoGenerate = true)
        private int id;
    ​
        @ColumnInfo(name = "english_word")
        private String word;
    ​
        @ColumnInfo(name = "chinese_mean")
        private String chineseMean;
    ​
        public Word(String word, String chineseMean) {
            this.word = word;
            this.chineseMean = chineseMean;
        }
    ​
        public int getId() {
            return id;
        }
    ​
        public void setId(int id) {
            this.id = id;
        }
    ​
        public String getWord() {
            return word;
        }
    ​
        public void setWord(String word) {
            this.word = word;
        }
    ​
        public String getChineseMean() {
            return chineseMean;
        }
    ​
        public void setChineseMean(String chineseMean) {
            this.chineseMean = chineseMean;
        }
    }

    • 必须在类前使用注解 @Entity 来声明。

    • 表结构中必须有一个主键,主键的声明为 @PrimaryKey ,而主键递增则在其后添加 (autoGenerate = true)。

    • 列名的注解使用 @ColumnInfo ,可以定义表结构中的列名,如 (name = "english_word") 。

    2、dao

    dao是一个接口,只需要写接口即可。

    package com.example.roombasic;
    ​
    import androidx.lifecycle.LiveData;
    import androidx.room.Dao;
    import androidx.room.Delete;
    import androidx.room.Insert;
    import androidx.room.Query;
    import androidx.room.Update;
    ​
    import java.util.List;
    ​
    @Dao
    public interface WordDao {
    ​
        @Insert
        void addWords(Word... words);
    ​
        @Update
        void updateWords(Word... words);
    ​
        @Delete
        void deleteWords(Word... words);
    ​
        @Query("delete from word")
        void deleteAllWords();
    ​
        @Query("select * from word order by id desc")
    //    List<Word> getAllWords();
        LiveData<List<Word>> getAllWordsLive();
    }


    • 同样需要使用注解来声明 @Dao 。

    • 每种接口需要使用注解来声明,如@Insert、@Update、@Delete。

    • @Query("select * from word order by id desc") 是查询语句。

    • 接口暂时不需要自己来实现,room已经帮我们写出了具体的代码。

    • Word... words 表明可以传进多个参数。类名... 对象名s 代表可以传递多个参数。

    3、database

    database来获得dao

    package com.example.roombasic;
    ​
    import android.content.Context;
    ​
    import androidx.room.Database;
    import androidx.room.Room;
    import androidx.room.RoomDatabase;
    @Database(entities = {Word.class},version = 1,exportSchema = false)
    public abstract class WordDatabase extends RoomDatabase {
        private static WordDatabase INSTANCE;
        static synchronized WordDatabase getDatabase(Context context){
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),WordDatabase.class,"word_database")
                        .build();
            }
            return INSTANCE;
        }
    ​
        public abstract WordDao getWordDao();
    }

    • 需要使用注解来声明 @Database(entities = {Word.class},version = 1,exportSchema = false)

      • entities = {Word.class}的{}中来填写entity,可添加多个。

      • version 是当前数据库版本。

      • exportSchema 暂时不知道干什么用,需要写上。

    • synchronized为java中的锁机制,多线程防止出错。

    • Room.databaseBuilder(context.getApplicationContext(),WordDatabase.class,"word_database").build

      • 第一个参数是activity,第二个参数为database的映射,第三个参数为数据库名称。

  • 相关阅读:
    Windows JScript 在 游览器 中运行 调试 Shell 文件系统
    autohotkey 符号链接 软连接 symbolink
    软链接 硬链接 测试
    SolidWorks 修改 基准面 标准坐标系
    手机 路径 WebDAV 映射 驱动器
    Win10上手机路径
    explorer 命令行
    单位公司 网络 封锁 屏蔽 深信 AC
    cobbler自动化部署原理篇
    Docker四种网络模式
  • 原文地址:https://www.cnblogs.com/wuren-best/p/12253217.html
Copyright © 2011-2022 走看看