安卓学习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的映射,第三个参数为数据库名称。
最新文章
- java反射学习之一反射机制概述
- NE Upgrade python script. Need to write a Tkinter GUI for it
- Gate level Simulation(门级仿真)
- Mysql --分区(3)range分区
- linux源码组织
- javascript进击(九)参考手册
- c - 向一个排序好的数组插入一个数,插入后数组依然是排序好的
- android数据保存
- sql解析xml
- js函数一些小的知识点
- POJ [P3660] Cow Contest
- es6常用语法学习笔记
- Idea使用Maven创建Java Web项目
- laravel 同数据表字段比较查询和状态不正规排序
- 并查集(Union-Find) 应用举例 --- 基础篇
- mac osx voice over的使用
- 如果恨一个程序员,忽悠他去做iOS开发
- 【Python】【有趣的模块】【Bobo】
- Spring学习笔记之The IoC container
- JS获取最终样式
热门文章
- tcpack---1简述
- WIN10下安装python3.7.2出现“尝试创建C:\Users\XX\AppData\Roaming\Microsoft\Installer时出错”
- parted分区对齐
- async await 你真的用对了吗?
- sqlilab less19-less22
- php(tp5)生成条形码
- First day,beginning!
- (i春秋 Misc)ReCreators - CryMisc
- div可以滚动但不显示滚动条
- SAP PI接口ESR IA配置,几种常用的 XSL 转换文档模板