当前位置:   article > 正文

Android 应用开发 Jetpack Room导入_jetpack 自增长id

jetpack 自增长id

Room框架同样基于SQL,但是通过注解技术极大简化了数据库操作,减少了原来的编码工作

在使用Room之前,要先修改模块的build.gradle文件,往

dependencies节点添加下面两行配置,表示导入指定版本的Room库。
implementation 'androidx.room:room-runtime:2.2.5'
annotationProcessor 'android.room:room-compiler:2.2.5'

Room框架编码步骤有以下五步

1.构建一个实体类保存书籍信息,注意打上注解Entity,对于id打上@PrimaryKey(autoGenerate = true)表示自动增长

  1. package com.example.chapter06.enity;
  2. import androidx.room.Entity;
  3. import androidx.room.PrimaryKey;
  4. @Entity
  5. public class BookInfo {
  6. @PrimaryKey(autoGenerate = true)
  7. private int id;
  8. private String name; // 书籍名称
  9. private String author; // 作者
  10. private String press; // 出版社
  11. private double price; // 价格
  12. public void setId(int id) {
  13. this.id = id;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public void setAuthor(String author) {
  19. this.author = author;
  20. }
  21. public void setPress(String press) {
  22. this.press = press;
  23. }
  24. public void setPrice(double price) {
  25. this.price = price;
  26. }
  27. public int getId() {
  28. return id;
  29. }
  30. public String getName() {
  31. return name;
  32. }
  33. public String getAuthor() {
  34. return author;
  35. }
  36. public String getPress() {
  37. return press;
  38. }
  39. public double getPrice() {
  40. return price;
  41. }
  42. @Override
  43. public String toString() {
  44. return "BookInfo{" +
  45. "id=" + id +
  46. ", name='" + name + '\'' +
  47. ", author='" + author + '\'' +
  48. ", press='" + press + '\'' +
  49. ", price=" + price +
  50. '}';
  51. }
  52. }

2.编写书籍信息表对应的持久化类,该类添加@Dao注解(Database access operator)

  1. package com.example.chapter06.dao;
  2. import androidx.room.Dao;
  3. import androidx.room.Delete;
  4. import androidx.room.Insert;
  5. import androidx.room.Query;
  6. import androidx.room.Update;
  7. import com.example.chapter06.enity.BookInfo;
  8. import java.util.List;
  9. @Dao
  10. public interface BookDao {
  11. @Insert
  12. void insert(BookInfo... book);
  13. @Delete
  14. void delete(BookInfo... book);
  15. @Query("DELETE FROM BookInfo")
  16. void deleteAll();
  17. @Update
  18. int update(BookInfo... book);
  19. @Query("SELECT * FROM BookInfo")
  20. List<BookInfo> queryAll();
  21. @Query("SELECT * FROM BookInfo WHERE name = :name ORDER BY id DESC limit 1")
  22. BookInfo queryByName(String name);
  23. }

 3.提供一个database

  1. package com.example.chapter06.database;
  2. import androidx.room.Database;
  3. import androidx.room.RoomDatabase;
  4. import com.example.chapter06.dao.BookDao;
  5. import com.example.chapter06.enity.BookInfo;
  6. //entities表示该数据库有哪些表,version表示数据库的版本号
  7. //exportSchema表示是否导出数据库信息的json串,建议设为false,若设为true还需指定json文件的保存路径
  8. @Database(entities = {BookInfo.class}, version = 1, exportSchema = true)
  9. public abstract class BookDataBase extends RoomDatabase {
  10. //
  11. public abstract BookDao bookDao();
  12. }
  13. //如果exportSchema 被设置为true了,需要在build.gradle中设置
  14. // javaCompileOptions {
  15. // annotationProcessorOptions {
  16. // arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]//指定数据库schema导出的位置
  17. // }
  18. // }

4.在自定义的Application类中声明书籍数据库的唯一实例

//构建数据库的实例
  1. bookDataBase = Room.databaseBuilder(this, BookDataBase.class, "book")
  2. // 允许迁移数据库(发生数据库变更时,Room默认删除原数据库再创建新数据库。如此一来原来的记录会丢失,故而要改为迁移方式以便保存原有记录)
  3. .addMigrations()
  4. //允许在主线程中操作数据库,(ROOM 默认不能在主线程中操作数据库)
  5. .allowMainThreadQueries().build();

5.获取实例

bookDao = MyApplication.getInstance().getBookDataBase().bookDao();

增删改查

  1. public void onClick(View view) {
  2. String name = et_name.getText().toString();
  3. String author = et_author.getText().toString();
  4. String press = et_press.getText().toString();
  5. String price = et_price.getText().toString();
  6. switch (view.getId()) {
  7. case R.id.btn_save:
  8. //以下声明一个书籍信息对象,并填写它的各个字段值
  9. BookInfo b1 = new BookInfo();
  10. b1.setName(name);
  11. b1.setAuthor(author);
  12. b1.setPress(press);
  13. b1.setPrice(Double.parseDouble(price));
  14. bookDao.insert(b1);
  15. ToastUtil.show(this, "保存成功");
  16. break;
  17. case R.id.btn_query:
  18. List<BookInfo> list = bookDao.queryAll();
  19. for (BookInfo b : list) {
  20. Log.d("xr", b.toString());
  21. }
  22. break;
  23. case R.id.btn_delete:
  24. BookInfo b2 = new BookInfo();
  25. b2.setId(1);
  26. bookDao.delete(b2);
  27. break;
  28. case R.id.btn_update:
  29. BookInfo b3 = new BookInfo();
  30. BookInfo b4 = bookDao.queryByName(name);
  31. b3.setId(b4.getId());
  32. b3.setName(name);
  33. b3.setAuthor(author);
  34. b3.setPress(press);
  35. b3.setPrice(Double.parseDouble(price));
  36. bookDao.update(b3);
  37. break;
  38. }
  39. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/785663
推荐阅读
相关标签
  

闽ICP备14008679号