添加依赖
implementation "android.arch.persistence.room:runtime:1.1.1"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
entity实体类
@Entity(tableName = "bill")
public class Bill {
@PrimaryKey
private int id;
@ColumnInfo(name = "type")
private int type;
@ColumnInfo(name = "type_name")
private String type_name;
@ColumnInfo(name = "money")
private double money;
@ColumnInfo(name = "date")
private String date;
@ColumnInfo(name = "remark")
private String remark;
public String getType_name() {
return type_name;
}
}
dao
@Dao
public interface BillDao {
@Query("SELECT * FROM bill")
List<Bill> getAll();
@Insert
void insertBill(Bill... bills);
@Delete
void delete(Bill bill);
@Update
void update(Bill bill);
@Query("DELETE FROM bill")
void deleteAll();
}
database
@Database(entities = {Bill.class}, version = 1)
public abstract class BillDatabase extends RoomDatabase {
public abstract BillDao billDao();
配置数据库的Schema的导出路径
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
注意:如果不需要导出,在定义database的时候加上:Schema @Database(entities = {Bill.class}, version = 1, exportSchema = false)