首页 > 数据库 >Room数据库(以book类为例)

Room数据库(以book类为例)

时间:2022-09-18 20:34:17浏览次数:99  
标签:Room author 类为例 private String book public name

使用 Room 实体定义数据 | Android 开发者 | Android Developers (google.cn)

build.gradle中加入以下引用

//room
def room_version = "2.4.3"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

建立实体Room

@Entity(tableName = "Book")
public class Book {
    @PrimaryKey
    private int Id;

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

    @ColumnInfo(name = "author")
    private String author;

    public int getId() {
        return Id;
    }
    public void setAll(int id,String name,String author){
        Id = id;
        this.author = author;
        this.name = name;
    }

    public void setId(int id) {
        Id = id;
    }

    public String getName() {
        return name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

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

建立Dao

@Dao
public interface BookDao {
    @Insert
    void insertAll(Book... book);

    @Delete
    void delete(Book book);

    @Query("SELECT * FROM book")
    List<Book> queryAll();

    @Query("SELECT name , author FROM book")
    public List<BookExact> findBook();
    //提取列的子集

    @Query("SELECT * FROM book WHERE name LIKE :search " + "OR author LIKE :search")
    public List<Book> findBookConnected(String search);
}

返回表格列的子集

public class BookExact {
    @ColumnInfo(name = "name")
    public String name;

    @ColumnInfo(name = "author")
    @NonNull
    public String author;

}

建立Database

@Database(entities = {Book.class},version = 1,exportSchema = false)
public abstract class BookDatabase extends RoomDatabase{

    private static final String DB_NAME = "BookDatabase.db";
    private static volatile BookDatabase instance;

    public static synchronized BookDatabase getInstance(Context context) {
        if (instance == null) {
            instance = create(context);
        }
        return instance;
    }

    private static BookDatabase create(final Context context) {
        return Room.databaseBuilder(
                context,
                BookDatabase.class,
                DB_NAME).build();
    }
    public abstract BookDao getUserDao();
}

使用数据库

private List<Book> bookExact;
private BookDatabase database;
private Book book = new Book();
database = BookDatabase.getInstance((Context) MainActivity.this);

子线程加入数据库

new Thread(new Runnable() {
    @Override
    public void run() {
        database.getUserDao().insertAll(book);
    }
}).start();

查询数据库

new Thread(new Runnable() {
    @Override
    public void run() {
        bookExact = database.getUserDao().queryAll();
        Integer x;
        if (bookExact == null ) x= 0;
        else  x= bookExact.size();
        Log.d("dlb",x.toString());
    }
}).start();

标签:Room,author,类为例,private,String,book,public,name
From: https://www.cnblogs.com/MarkDespite/p/16705676.html

相关文章