赞
踩
Room是一个SQLite抽象层,使用更加简单,通过注解的方式标记相关功能,编译时自动生成响应的 impl 实现类,具有丰富的编译时校验,错误提示。
类注解:
类内注解:
implementation "androidx.room:room-common:2.4.3"
annotationProcessor "androidx.room:room-compiler:2.4.3"
implementation "androidx.room:room-runtime:2.4.3"
@Entity(tableName = "Fruit") public class Fruit { @PrimaryKey @NonNull public Integer id; @ColumnInfo(name = "fruit_name") public String fruitName; @ColumnInfo(name = "fruit_type") public String fruitType; @ColumnInfo(name = "fruit_price") public Integer fruitPrice; @ColumnInfo(name = "fruit_num") public Integer fruitNum; @Ignore public Fruit(Integer id, String fruitName, String fruitType, Integer fruitPrice, Integer fruitNum) { this.id = id; this.fruitName = fruitName; this.fruitType = fruitType; this.fruitPrice = fruitPrice; this.fruitNum = fruitNum; } public Fruit() { } @NonNull public Integer getId() { return id; } public void setId(@NonNull Integer id) { this.id = id; } public String getFruitName() { return fruitName; } public void setFruitName(String fruitName) { this.fruitName = fruitName; } public String getFruitType() { return fruitType; } public void setFruitType(String fruitType) { this.fruitType = fruitType; } public Integer getFruitPrice() { return fruitPrice; } public void setFruitPrice(Integer fruitPrice) { this.fruitPrice = fruitPrice; } public Integer getFruitNum() { return fruitNum; } public void setFruitNum(Integer fruitNum) { this.fruitNum = fruitNum; } @Override public String toString() { return "Fruit{" + "id=" + id + ", fruitName='" + fruitName + '\'' + ", fruitType='" + fruitType + '\'' + ", fruitPrice=" + fruitPrice + ", fruitNum=" + fruitNum + '}'; } }
@Dao public interface MyDAO { /**参数可为List**/ //返回插入的 rowId 值,long 型 @Insert Long insertFruit(Fruit fruit); //插入列表 @Insert List<Long> insertList(List<Fruit> list); //返回受影响的行数 @Delete int deleteFruit(Fruit fruit); //返回受影响的行数 @Update(entity = Fruit.class) int updateFruit(Fruit fruit); //返回查询的数据 @Query("select * from fruit;") List<Fruit> selectAllFruit(); }
@Database(version = 1,entities = {Fruit.class},exportSchema = false)
public abstract class MyDatabase extends RoomDatabase {
public abstract MyDAO myDAO();
}
new Thread(() -> { MyDatabase database = Room.databaseBuilder(getApplicationContext(), MyDatabase.class, "Fruit").build(); MyDAO dao = database.myDAO(); try { //插入单个 dansertFruit(new Fruit(1, "橘子", "橘子", 10, 120)); //插入 List List<Fruit> entityList = new ArrayList<>(); for (int i = 0; i < 20; i++) { Fruit entity = new Fruit(i, "" + i, "" + i, i, i); entityList.add(entity); } dao.insertList(entityList); } catch (SQLiteConstraintException e) { //id 为 1 那一项会导致异常,因为数据库已经存在该主键数据 Log.e(TAG, "There are same data in local!!!"); } Log.d(TAG, "insert success,size:"); //查找 Fruit List<Fruit> fruits = dao.selectAllFruit(); Fruit fruit = new Fruit(); if (fruits.size() > 0) { fruit = fruits.get(0); } Log.d(TAG, "初始数据库中Fruit为:" + fruit); //修改 Fruit fruit.setFruitName("苹果"); fuit.setFruitType("苹果"); dao.updateFruit(fruit); //查找 Fruit List<Fruit> fruitList = dao.selectAllFruit(); Fruit f = new Fruit(); if (fruits.size() > 0) { fruit = fruits.get(0); } Log.i(TAG, "修改后的数据 Fruit 为:" + f); //删除 Fruit dao.deleteFruit(f); //查找 Fruit List<Fruit> list = dao.selectAllFruit(); Fruit fi = new Fruit(); if (fruits.size() > 0) { fruit = fruits.get(0); } Log.d(TAG, "删除后数据库中Fruit为:" + fi); }).start();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。