Room数据库操作

Room

注解:对sqlite数据库的封装

导入Room支持://build.gradle

 

//room
def room_version="2.2.0-alpha01"
implementation "androidx.room:room-runtime:$room_version" //room Api支持
annotationProcessor "androidx.room:room-compiler:$room_version" //room的注解处理器

Room 三大角色

  1. Entity

    package com.neostra.anyan.test;

    import androidx.annotation.NonNull;
    import androidx.room.Entity;
    import androidx.room.PrimaryKey;

    @Entity//表实体类
    public class Student {
       @PrimaryKey(autoGenerate = true)
       private int _id;
       private String name;

       public Student() {
      }

       public int get_id() {
           return _id;
      }

       public void set_id(int _id) {
           this._id = _id;
      }

       public String getName() {
           return name;
      }

       public void setName(String name) {
           this.name = name;
      }

       @NonNull
       @Override
       public String toString() {
           return super.toString();
      }
    }
  2. DAO

package com.neostra.anyan.test;

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//实现对Student表的增删改查
public interface StudentDao {

   @Insert//增
   void insertStudents(Student ... students);
   @Delete//只支持删除单个数据
   void deleteStudents(Student... students);
   @Query("delete from Student")//删除表中所有数据
   void deleteAllStudents(Student... students);
   @Update//更新
   void upadateStudents(Student... students);
   @Query("select * from student order by _id asc")//按序查询,返回list
   List<Student> getAllStudents();
}
  1. Database

    package com.neostra.anyan.test;

    import android.content.Context;

    import androidx.room.Database;
    import androidx.room.Room;
    import androidx.room.RoomDatabase;
    //将表和操作表的DAO联系起来
    @Database(entities = {Student.class},version =1,exportSchema = false)
    public abstract class StudentDatabase extends RoomDatabase {
       //单例模式 返回DB
       private static StudentDatabase INSTANCE;
       static synchronized StudentDatabase getINSTANCE(Context context){
           if (INSTANCE==null){
               INSTANCE= Room.databaseBuilder(context.getApplicationContext(),StudentDatabase.class,"student_database")
                       /*如果数据库默认是异步线程,强制开启主线程操作数据库,此方法谨慎使用,仅测试过程使用*/
                      .allowMainThreadQueries()
                      .build();
          }
           return INSTANCE;
      }
       //为用户提供操作数据库的DAO,当用户需要操作数据库时只需要操作DAO即可
       public abstract StudentDao getStudentDao();
    }

 

//room
def room_version="2.2.0-alpha01"
implementation "androidx.room:room-runtime:$room_version" //room Api支持
annotationProcessor "androidx.room:room-compiler:$room_version" //room的注解处理器
posted @   安妍  阅读(218)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
历史上的今天:
2021-04-11 foreach语法
2021-04-11 哈希表
点击右上角即可分享
微信分享提示