当前位置:   article > 正文

Android--Jetpack--数据库Room详解一_安卓 room数据库

安卓 room数据库

人生何须万种愁,千里云烟一笑收

一,定义

Room也是一个ORM框架,它在SQLite上提供了一个抽象层,屏蔽了部分底层的细节,使用对象对数据库进行操作,进行CRUD就像对象调用方法一样的简单。

二,角色介绍

谷歌官方给出了一张图片,可以更加直观的了解Room的组成部分:

 从上图可以看出,Room主要由三部分组成:

1,Room Database 数据库:底层连接的主要接入点,创建数据库就靠它了

2,Data Access Objects DAO:在DAO中会有一系列对数据库进行CRUD的方法声明

3,Entity 实体类:是对象与数据表的对应表现,设计实体类,并最后转化为对应的数据表

可以这么理解,我们通过实体类的中Getter和Setter方法对数据进行变更操作,然后Room Database使用DAO中的方法,对表中的数据进行对应的操作。从而屏蔽了繁琐的数据库原生操作。当然这其中使用了注解进行功能的标注。

三,基本使用

1,在app的build.gradle里面添加依赖:

  1. def room_version = "2.2.6"
  2. implementation "androidx.room:room-runtime:$room_version"
  3. annotationProcessor "androidx.room:room-compiler:$room_version" // use kapt for Kotlin
  4. // optional - RxJava support for Room
  5. implementation "androidx.room:room-rxjava2:$room_version"
  6. // optional - Guava support for Room, including Optional and ListenableFuture
  7. implementation "androidx.room:room-guava:$room_version"
  8. // Test helpers
  9. testImplementation "androidx.room:room-testing:$room_version"

2,创建表Entity:

  1. @Entity
  2. public class YuanZhen {
  3. @PrimaryKey(autoGenerate = true)
  4. private int id;
  5. @ColumnInfo(name ="name")
  6. private String name;
  7. @ColumnInfo(name ="age")
  8. private int age;
  9. @ColumnInfo(name ="address")
  10. private String address;
  11. @Ignore
  12. private String sex;
  13. public YuanZhen(String name, int age, String address) {
  14. this.name = name;
  15. this.age = age;
  16. this.address = address;
  17. }
  18. public void setId(int id) {
  19. this.id = id;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. public void setAge(int age) {
  25. this.age = age;
  26. }
  27. public void setAddress(String address) {
  28. this.address = address;
  29. }
  30. public int getId() {
  31. return id;
  32. }
  33. public String getName() {
  34. return name;
  35. }
  36. public int getAge() {
  37. return age;
  38. }
  39. public String getAddress() {
  40. return address;
  41. }
  42. @Override
  43. public String toString() {
  44. return "YuanZhen{" +
  45. "id=" + id +
  46. ", name='" + name + '\'' +
  47. ", age=" + age +
  48. ", address='" + address + '\'' +
  49. ", sex='" + sex + '\'' +
  50. '}';
  51. }
  52. }

上面其中几个注解的含义:

①,@Entity 表示数据库中的表

②,@PrimaryKey 表示主键,autoGenerate 表示自增

③,@ColumnInfo 表示字段,name 表示字段名称

④,@Ignore 表示一个属性不加入生成表的字段,只是临时使用

3,创建Dao:

  1. @Dao
  2. public interface YuanZhenDao {
  3. @Insert
  4. void insert(YuanZhen... yuanzhens);
  5. @Delete
  6. void delete(YuanZhen yuanZhen);
  7. @Update
  8. void update(YuanZhen yuanZhen);
  9. @Query("select * from YuanZhen")
  10. List<YuanZhen> getAll();
  11. }

这里面包含了增上改查。

其中几个注解的含义:

①,@Dao 表示访问 DB 的方法,需要声明为接口或抽象类,编译阶段将生成 _Impl 实现类,此处则将生成 YuanZhenDao_Impl.java 文件

②,@Insert、@Delete、@Update 、 @Query 分别表示数据库的增删改查方法

4,创建Room Database 数据库:

  1. @Database(entities = {YuanZhen.class},version = 1,exportSchema = false)
  2. public abstract class MyDatabase extends RoomDatabase {
  3. public abstract YuanZhenDao yuanZhenDao();
  4. }

其中几个注解的含义:

①,@Database:表示数据库的定义

②,entities: 表示数据库中包含的表

③,version:表示数据库版本号

5,使用:

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. new Thread(new Runnable() {
  7. @Override
  8. public void run() {
  9. MyDatabase myDatabase = Room.databaseBuilder(getApplicationContext(),MyDatabase.class,"YuanZhenDb").build();
  10. YuanZhenDao yuanZhenDao = myDatabase.yuanZhenDao();
  11. yuanZhenDao.insert(new YuanZhen("yz",20,"淄博市"));
  12. yuanZhenDao.insert(new YuanZhen("yz1",22,"淄博市"));
  13. List<YuanZhen> all = yuanZhenDao.getAll();
  14. System.out.println("输出:"+all.toString());
  15. }
  16. }).start();
  17. }
  18. }

输出结果:

I/System.out: 输出:[YuanZhen{id=1, name='yz', age=20, address='淄博市', sex='null'}, YuanZhen{id=2, name='yz1', age=22, address='淄博市', sex='null'}, YuanZhen{id=3, name='yz', age=20, address='淄博市', sex='null'}

这就是基本使用。

四,条件查询

如果我们想查询某一条数据的话,需要在dao里面增加条件查询:

  1. @Dao
  2. public interface YuanZhenDao {
  3. @Insert
  4. void insert(YuanZhen... yuanzhens);
  5. @Delete
  6. void delete(YuanZhen yuanZhen);
  7. @Update
  8. void update(YuanZhen yuanZhen);
  9. @Query("select * from YuanZhen")
  10. List<YuanZhen> getAll();
  11. @Query("select * from YuanZhen where name like :name")
  12. YuanZhen getByName(String name);
  13. @Query("select * from YuanZhen where age in(:ages)")
  14. List<YuanZhen> getByAges(int[] ages);
  15. }

使用:

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. new Thread(new Runnable() {
  7. @Override
  8. public void run() {
  9. MyDatabase myDatabase = Room.databaseBuilder(getApplicationContext(),MyDatabase.class,"YuanZhenDb").build();
  10. YuanZhenDao yuanZhenDao = myDatabase.yuanZhenDao();
  11. yuanZhenDao.insert(new YuanZhen("yz",20,"淄博市"));
  12. yuanZhenDao.insert(new YuanZhen("yz1",22,"淄博市"));
  13. YuanZhen yz = yuanZhenDao.getByName("yz");
  14. System.out.println("输出ByName:"+yz.toString());
  15. int[] ages ={20,22};
  16. List<YuanZhen> byAges = yuanZhenDao.getByAges(ages);
  17. System.out.println("输出ByAges:"+byAges.toString());
  18. }
  19. }).start();
  20. }
  21. }

输出:

I/System.out: 输出ByName:YuanZhen{id=1, name='yz', age=20, address='淄博市', sex='null'}
I/System.out: 输出ByAges:[YuanZhen{id=1, name='yz', age=20, address='淄博市', sex='null'}, YuanZhen{id=2, name='yz1', age=22, address='淄博市', sex='null'}

如果只想查姓名和地址,那么可以新建一个类:

  1. public class YuanZhenNew {
  2. @ColumnInfo(name = "name")
  3. public String name;
  4. @ColumnInfo(name = "address")
  5. public String address;
  6. public void setName(String name) {
  7. this.name = name;
  8. }
  9. public void setAddress(String address) {
  10. this.address = address;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public String getAddress() {
  16. return address;
  17. }
  18. public YuanZhenNew(String name, String address) {
  19. this.name = name;
  20. this.address = address;
  21. }
  22. @Override
  23. public String toString() {
  24. return "YuanZhenNew{" +
  25. "name='" + name + '\'' +
  26. ", address='" + address + '\'' +
  27. '}';
  28. }
  29. }

在dao中查询:

  1. @Dao
  2. public interface YuanZhenDao {
  3. @Insert
  4. void insert(YuanZhen... yuanzhens);
  5. @Delete
  6. void delete(YuanZhen yuanZhen);
  7. @Update
  8. void update(YuanZhen yuanZhen);
  9. @Query("select * from YuanZhen")
  10. List<YuanZhen> getAll();
  11. @Query("select * from YuanZhen where name like :name")
  12. YuanZhen getByName(String name);
  13. @Query("select * from YuanZhen where age in(:ages)")
  14. List<YuanZhen> getByAges(int[] ages);
  15. @Query("select name,address from YuanZhen ")
  16. public List<YuanZhenNew> getNew();
  17. }

使用:

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. new Thread(new Runnable() {
  7. @Override
  8. public void run() {
  9. MyDatabase myDatabase = Room.databaseBuilder(getApplicationContext(),MyDatabase.class,"YuanZhenDb").build();
  10. YuanZhenDao yuanZhenDao = myDatabase.yuanZhenDao();
  11. yuanZhenDao.insert(new YuanZhen("yz",20,"淄博市"));
  12. yuanZhenDao.insert(new YuanZhen("yz1",22,"淄博市"));
  13. List<YuanZhenNew> aNew = yuanZhenDao.getNew();
  14. System.out.println("输出aNew:"+aNew.toString());
  15. }
  16. }).start();
  17. }
  18. }

输出:

I/System.out: 输出aNew:[YuanZhenNew{name='yz', address='淄博市'}, YuanZhenNew{name='yz1', address='淄博市'}]

更多高级用法参考文章:Android--Jetpack--数据库Room详解二-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/743060
推荐阅读
相关标签
  

闽ICP备14008679号