1.Room入门小例子

首先先导入依赖

	def room_version = "2.2.0"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    // optional - Test helpers
    testImplementation "androidx.room:room-testing:$room_version"

写实体类 WordEntity

package com.example.myroombasic;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity
public class WordEntity {
    
    

    @PrimaryKey(autoGenerate = true)
    private int id;

    @ColumnInfo(name = "english word")
    private String word;

    private String chineseMeaning;

    public WordEntity(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 chineseMeaning;
    }

    public void setChineseMeaning(String chineseMeaning) {
    
    
        this.chineseMeaning = chineseMeaning;
    }

    @Override
    public String toString() {
    
    
        return "WordEntity{" +
                "id=" + id +
                ", word='" + word + '\'' +
                ", chineseMeaning='" + chineseMeaning + '\'' +
                '}';
    }
}

写数据库操作接口 WordDao

package com.example.myroombasic;

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 insertWords(WordEntity...WordEntity);

    @Update
    void updateWord(WordEntity...WordEntity);

    @Delete
    void deleteWords(WordEntity...WordEntity);

    //删除全部  数据库名写实体类名就行了
    @Query("delete from wordentity")
    void deleteAllWords();

    //查询全部 数据库名写实体类名就行了
    @Query("select * from WordEntity order by id desc")
    List<WordEntity> getAllWords();
}

写创建数据库的抽象类

package com.example.myroombasic;

import androidx.room.Database;
import androidx.room.RoomDatabase;

@Database(entities = {
    
    WordEntity.class},version = 1,exportSchema = false)
public abstract class WordDataBase extends RoomDatabase {
    
    
    public abstract WordDao getWordDao();
}

主函数

package com.example.myroombasic;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    
    

    WordDataBase wordDataBase;
    WordDao wordDao;
    TextView textView66;
    String text;

    Button buttonInsert,buttonDelete,buttonClear,buttonUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //各个组件的初始化
        textView66 = findViewById(R.id.text66);
        buttonInsert = findViewById(R.id.buttonInsert);
        buttonClear = findViewById(R.id.buttonClear);
        buttonUpdate = findViewById(R.id.buttonUpdate);
        buttonDelete = findViewById(R.id.buttonDelete);

        //创建数据库
        // allowMainThreadQueries强迫在主线程运行
        //正常来说是不允许在主线程中运行的
        wordDataBase = Room.databaseBuilder(this,WordDataBase.class,"fuck")
                .allowMainThreadQueries()
                .build();

        wordDao = wordDataBase.getWordDao();

        //显示数据库的内容
        updateView();

        //插入按钮
        buttonInsert.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
                WordEntity wordEntity = new WordEntity("hello", "你好");
                WordEntity wordEntity1 = new WordEntity("world", "世界");
                wordDao.insertWords(wordEntity,wordEntity1);
                updateView();
            }
        });

        //清空按钮
        buttonClear.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
                wordDao.deleteAllWords();
                updateView();
            }
        });

        //更新按钮
        buttonUpdate.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
                WordEntity wordEntity = new WordEntity("更改id为64的", "更改id为64的");
                wordEntity.setId(64);
                wordDao.updateWord(wordEntity);
                updateView();
            }
        });

        //删除按钮
        buttonDelete.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
                wordDao.deleteWords();
            }
        });


    }

    //更新视图
    void updateView(){
    
    
        text = "";
        List<WordEntity> allWords = wordDao.getAllWords();
        for (WordEntity allWord : allWords) {
    
    
            text += allWord.toString() + "  ";
        }
        textView66.setText(text);
    }
}

猜你喜欢

转载自blog.csdn.net/u011090423/article/details/120647472