Android Studio 之 ROM【1】, Entity,Dao,Database
Android Studio 之 ROM, Entity,DAO,DataBase
1.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="englist_word")
private String word; @ColumnInfo(name="chinese_meaning")
private String chineseMeaning; public Word(String word, String chineseMeaning) {
this.word = word;
this.chineseMeaning = chineseMeaning;
} 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 getChineseMeaning() {
return this.chineseMeaning;
} public void setChineseMeaning(String chineseMeaning) {
this.chineseMeaning = chineseMeaning;
}
}
2.Dao 接口
package com.example.roombasic; 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 //Database access object
public interface WordDao {
@Insert
void insertWords(Word ... words); //如果只插入一条记录,用long返回值,返回id @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(); }
3.Database 抽象类
package com.example.roombasic; import androidx.room.Database;
import androidx.room.RoomDatabase; @Database(entities = {Word.class},version = 1,exportSchema = false) //如果有多个实体,再加上逗号后加实体.Class,这里版本比较重要,每次更新都要改变
public abstract class WordDatabase extends RoomDatabase {
//抽象类
public abstract WordDao getWrodDao();
}
4.MainActity 类
package com.example.roombasic; import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room; import java.util.List; public class MainActivity extends AppCompatActivity { WordDatabase wordDatabase;
WordDao wordDao;
Button buttonInsert,buttonUpdate,buttonClear,buttonDelete;
TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wordDatabase = Room.databaseBuilder(this,WordDatabase.class,"word_database").allowMainThreadQueries().build(); //allowMainThreadQueries() 强制允许在主线程运行
wordDao = wordDatabase.getWrodDao(); textView = findViewById(R.id.textView);
buttonInsert = findViewById(R.id.buttonInsert);
buttonUpdate = findViewById(R.id.buttonUpdate);
buttonClear = findViewById(R.id.buttonClear);
buttonDelete = findViewById(R.id.buttonDelete); buttonInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Word word1 = new Word("Hello","你好");
Word word2 = new Word("World","世界"); wordDao.insertWords(word1,word2);
updateView();
}
}); buttonUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Word word = new Word("English","英语");
word.setId(23); //更新是用主键来更新的
wordDao.updateWords(word);
updateView();
}
}); //删除所有的记录
buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wordDao.deleteAllWords();
updateView();
}
}); buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Word word = new Word("English","英语");
word.setId(23); //删除也是用主键来更新的
wordDao.deleteWords(word);
updateView();
}
}); } void updateView(){
List<Word> list = wordDao.getAllWords();
String text="";
textView.setText(text); //先将 textView 清空
for(int i=0;i<list.size();i++){
Word word = list.get(i);
text += word.getId() + ":" + word.getWord() + "=" + word.getChineseMeaning() + "\n"; textView.setText(text);
}
} }
Android Studio 之 ROM【1】, Entity,Dao,Database的更多相关文章
- Android Studio 之 ROM【3】,LiveData+ViewModel+AsyncTask+Repository+RecyclerView
教程地址:https://www.bilibili.com/video/av65180549 源码地址:https://github.com/longway777/Android-2019-Tutor ...
- Android Studio 之 ROM【2】, LiveData+ViewModel+AsyncTask+Repository
改造上一节 ROM[1], 1.利用 LiveData<List<Word>> 与 observe 中的 onChanged 配合,删除掉之前的textView更新函数(upd ...
- [转]Android Studio SQLite Database Multiple Tables Example
本文转自:http://instinctcoder.com/android-studio-sqlite-database-multiple-tables-example/ BY TAN WOON HO ...
- [转]Android Studio SQLite Database Example
本文转自:http://instinctcoder.com/android-studio-sqlite-database-example/ BY TAN WOON HOW · PUBLISHED AP ...
- Android GreenDao with Android Studio IDE
转:http://blog.surecase.eu/using-greendao-with-android-studio-ide/ In this tutorial we will show you ...
- how to use greendao in android studio
http://www.arjunsk.com/android/use-greendao-android-studio/ 1.新建一个java文件MainGenerator.java: import d ...
- Android Studio配置GreenDAO 3.2.0和使用方法
我相信,在平时的开发过程中,大家一定会或多或少地接触到SQLite.然而在使用它时,我们往往需要做许多额外的工作,像编写SQL语句与解析查询结果等.所以,适用于Android ORM框架也就孕育而生了 ...
- [Android Studio]SQLScout插件安装破解
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5972138.html [Android Studio]SQLS ...
- Android项目实战(二十五):Android studio 混淆+打包+验证是否成功
前言: 单挑Android项目,最近即时通讯用到环信,集成sdk的时候 官方有一句 在 ProGuard 文件中加入以下 keep. -keep class com.hyphenate.** {*;} ...
随机推荐
- 极简 Spring Boot 整合 Thymeleaf 页面模板
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
- Java 8——接口中个的默认方法和静态方法
在Java SE 8之前,interface只是事物的抽象,用来定义统一的抽象事物和描述事物的抽象行为和属性. 但是在Java SE 8中,增加了可以在interface中增加默认实现的行为和事物的静 ...
- Java中Deque特性及API
美人如斯,文章如斯! 定义 双向队列:支持插入删除元素的线性集合 特性: 插入.删除.获取操作支持两种形式:快速失败和返回null或true/false 既具有FIFO特点又具有LIFO特点,即是队列 ...
- WPF 精修篇 路径动画
原文:WPF 精修篇 路径动画 路径动画 是让一个对象围绕指定Path 的运动路径 进行移动的动画 举栗子 路径动画 使用 Blend 来设置 是十分简单的 首先用工具 笔 点出一条线 新建一个圆形 ...
- mysql 查询数据库表信息,字段信息
#======================================================================= #查询表信息 select table_name, t ...
- Macro的写法 `( , ,@ )
另外的注意点: 1. 同名符号的 “变量捕捉” (varible capture) 解决方式: with-gensym 生成几个unique name-s, 然后将它们各自绑定上参数值 2. 多次 ...
- ThinkPHP各个目录是什么含义ThinkPHP怎么安装和使用
最近kdchxue看完了smarty之后,想学习下框架,于是乎就选择了ThinkPHP,听说这个框架简单易用,另外还是国产的!所以kdchxue毫不犹豫的就选择了ThinkPHP 了!下面看看Thin ...
- git操作:删除仓库中的文件或目录
假定当前分支下,abc/123.txt需要从git仓库中删除: git .txt //删除abc目录下的123.txt文件,如果要删除abc目录,使用命令:git rm -r --cached abc ...
- ES6兼容ie9, flex兼容ie9
vue兼容ES6 在 ie9 的环境上,es6 的部分新对象.表达式,并不支持,解决方案是使用 babel-polyfill 组件,它可以将 es6 的代码翻译成低版本浏览器可以识别的 es5 代码 ...
- pdf.js实现图片在线预览
项目需求 前段时间项目中遇到了一个模块,是关于在线预览word文档(PDF文件)的,所以,找了很多插件,例如,pdf.js,pdfobject.js框架,但是pdfobject.js框架对于IE浏览器 ...