介绍
Room在SQLite之上提供了一个抽象层,可以在使用SQLite的全部功能的同时流畅访问数据库。
@Database: @Database用来注解类,并且注解的类必须是继承自RoomDatabase的抽象类。该类主要作用是创建数据库和创建Daos(data access objects,数据访问对象)。
@Entity: @Entity 用来注解实体类, @Database 通过entities属性引用被 @Entity注解的类,并利用该类的所有字段作为表的列名来创建表。
@Dao: @Dao用来注解一个接口或者抽象方法,该类的作用是提供访问数据库的方法。在使用 @Database注解的类中必须定一个不带参数的方法,这个方法返回使用 @Dao注解的类。
应用
Android TV APP,给客户做一个EPG功能模块,可以预约EPG。
- 导入Room库
allprojects {
repositories {
jcenter()
maven {url 'https://maven.google.com' }
}
}
// Room Persistence Library
compile 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
- 创建表(表名、字段根据注释一目了然)
@Entity(tableName = "epg")
public class EPG {
public EPG(String program, int channelID, String start, String end) {
this.program = program;
this.channelID = channelID;
this.start = start;
this.end = end;
}
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "epgProgram")
@SerializedName("epgProgram")
private String program;
@ColumnInfo(name = "epgChannelID")
@SerializedName("epgChannelID")
private int channelID;
@ColumnInfo(name = "epgStart")
@SerializedName("epgStart")
private String start;
@ColumnInfo(name = "epgEnd")
@SerializedName("epgEnd")
//set、get方法省略
...
}
@Entity(tableName = "bookprogram")
public class BookProgram {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "channel")
@SerializedName("channel")
private String channel;
@ColumnInfo(name = "program")
@SerializedName("program")
private String program;
@ColumnInfo(name = "starttamp")
@SerializedName("starttamp")
private Long starttamp;
@ColumnInfo(name = "streamUrl")
@SerializedName("streamUrl")
private String streamUrl;
//set、get方法省略
...
}
- DAO类
@Dao
public interface EpgDAO {
@Query("SELECT * FROM epg")
List<EPG> getAll();
@Query("SELECT * FROM epg where epgChannelID = :channelid AND epgEnd > :timestamp AND epgStart < :timestamp ORDER BY epgEnd ASC LIMIT 1")
EPG findNowByChannel(int channelid, long timestamp);
@Query("SELECT * FROM epg where epgChannelID = :channelid AND epgStart > :timestamp ORDER BY epgEnd ASC LIMIT 1")
EPG findNextByChannel(int channelid, long timestamp);
@Query("SELECT * FROM epg where epgChannelID = :channelid ORDER BY epgEnd")
List<EPG> findEPGByChannel(int channelid);
@Query("SELECT COUNT(*) from epg")
int countEPGs();
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(EPG... epgs);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<EPG> epgs);
@Delete
void delete(EPG epg);
@Query("DELETE FROM epg")
void clearAll();
}
@Dao
public interface BookProgramDAO {
@Query("SELECT * FROM bookprogram")
List<BookProgram> getAllBookProgram();
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertBookProgram(BookProgram bookProgram);
@Query("DELETE FROM bookprogram WHERE program = :program AND starttamp = :starttamp")
void deleteBookProgram(String program,long starttamp);
@Query("DELETE FROM bookprogram")
void clearAll();
}
- Database类
@Database(entities = {EPG.class,BookProgram.class}, version = 1,exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract EpgDAO epgDao();
public abstract BookProgramDAO bookProgramDao();
public static AppDatabase getAppDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "itvdatabase")
// allow queries on the main thread.
// Don't do this on a real app! See PersistenceBasicSample for an example.
.allowMainThreadQueries()
.fallbackToDestructiveMigration()
.build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
使用时获取AppDatabse单例,调用成员DAO的接口即可