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的映射,第三个参数为数据库名称。

安卓学习02---room的更多相关文章

  1. JavaScript学习02 基础语法

    JavaScript学习02 基础语法 JavaScript中很多基础内容和Java中大体上基本一样,所以不需要再单独重复讲了,包括: 各种算术运算符.比较运算符.逻辑运算符: if else语句.s ...

  2. Java虚拟机JVM学习02 类的加载概述

    Java虚拟机JVM学习02 类的加载概述 类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个java.lang.Class对 ...

  3. Python学习02 列表 List

    Python学习02 列表 List Python列表 List Python中的列表(List)用逗号分隔,方括号包围(comma-separated values (items) between ...

  4. Android Testing学习02 HelloTesting 项目建立与执行

    Android Testing学习02 HelloTesting 项目建立与执行 Android测试,分为待测试的项目和测试项目,这两个项目会生成两个独立的apk,但是内部,它们会共享同一个进程. 下 ...

  5. Java学习02

    Java学习02 1.导入内部的包 一.在包的下面加入下面一句话: import    java.util.Scanner; 二.在类中 Scanner input=new     Sanner(Sy ...

  6. ThinkPhp学习02

    原文:ThinkPhp学习02 一.什么是MVC                M -Model 编写model类 对数据进行操作 V -View  编写html文件,页面呈现 C -Controll ...

  7. JVM学习02:GC垃圾回收和内存分配

    JVM学习02:GC垃圾回收和内存分配 写在前面:本系列分享主要参考资料是  周志明老师的<深入理解Java虚拟机>第二版. GC垃圾回收和内存分配知识要点Xmind梳理 案例分析1-(G ...

  8. 2018.12.22 Spring学习02

    Spring学习02 1.使用注解配置Spring 1.1 为主配置文件引入新的命名空间(约束) 添加约束文件xxx-xxx-context.xml 添加到主配置文件中 选择刚才的context.xm ...

  9. 天脉2(ACoreOS653)操作系统学习02

    天脉2(ACoreOS653)操作系统学习02 一.分区内通信方法 分区内通信指同一分区内进程之间的通信.ARINC 653定义的分区内进程通信机制,包括:缓存队列(Buffers-Queue).黑板 ...

随机推荐

  1. Spider--实战--selenium_12306

    # login12306_02 # 图像识别涉及到深度学习,这里直接将验证码识别任务发送到大佬的验证码解析地址,不过现在已经失效了,程序跑到这会报错. # 用户名和密码存储在本地工作目录中的 user ...

  2. FreeSql接入CAP的实践

    CAP CAP 是一个基于 .NET Standard 的 C# 库,它是一种处理分布式事务的解决方案,同样具有 EventBus 的功能,它具有轻量级.易使用.高性能等特点. https://git ...

  3. 集成学习算法——adaboost

    adaboost是boosting类集成学习方法中的一种算法,全称是adaptive boost,表示其是一种具有自适应性的算法,这个自适应性体现在何处,下面来详细说明. 1.adaboost算法原理 ...

  4. python之 《zip,lambda, map》

    1.zip 对于zip我们一般都是用在矩阵上 eg: a = [1,2,3] b = ['a', 'b', 'c'] x = zip(a, b) print(x) print(list(x)) 结果是 ...

  5. Python_获取cookie

    获取cookie from selenium import webdriver from selenium.webdriver.common.by import By # 定位 from seleni ...

  6. Android 滑动删除控件推荐

    implementation 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.3.0' <?xml version="1.0" enc ...

  7. c++11-17 模板核心知识(八)—— enable_if<>与SFINAE

    引子 使用enable_if<>禁用模板 enable_if<>实例 使用Concepts简化enable_if<> SFINAE (Substitution Fa ...

  8. 差点跪了!阿里3面真题:CAP和BASE理论了解么?可以结合实际案例说下不?

    本文节选自我开源的 JavaGuide :https://github.com/Snailclimb/JavaGuide (Github标星92k+!一份涵盖大部分 Java 程序员所需要掌握的核心知 ...

  9. C++深拷贝与浅拷贝区别

    浅拷贝只是对指针的拷贝,浅拷贝后两个指针指向同一个内存空间: 深拷贝不仅对指针进行拷贝,而且对指针指向的内容进行拷贝,经深拷贝后的指针是指向两个不同地址的指针. 当对一个已知对象进行拷贝时,编译系统会 ...

  10. Java的BigDecimal,对运算封装

    添加maven依赖 <dependency> <groupId>com.google.guava</groupId> <artifactId>guava ...