• android Room初步使用


    现在比较流行google官方的JetPack(喷气背包),这是个什么东东呢?

    其实就是谷歌官方的一套工具包,提供各种工具类,因此可以部分不用第三方的开源库了,

    比如:Room,Room是安卓设备SqlLite的框架,以前也用过不少类似的第三方开源框架,比如:

    GreenDao、Realm;

    Realm不推荐使用,真难用,谁用谁知道!

    GreenDao还不错!

    既然Room是大势所趋,那么学一学它该如何使用吧;

    Room 持久性库  |  Android 开发者  |  Android Developers

    目前最高版本是2.4.3,如果用这个版本的话,compileSdk最低要求31,这里就暂时不用最高版本,以后再用;

    主module中build.gradle文件,加上依赖:

    1. implementation 'androidx.room:room-runtime:2.1.0-alpha03'
    2. annotationProcessor 'androidx.room:room-compiler:2.1.0-alpha03'
    3. implementation 'androidx.room:room-rxjava2:2.1.0-alpha03'

     数据库包结构如下:

    AppDatabase.java文件(创建数据库名称、实例等):

    1. package com.example.testgps.database;
    2. import android.content.Context;
    3. import com.example.testgps.bean.SignRecord;
    4. import com.example.testgps.database.dao.BasicDataDao;
    5. import androidx.room.Database;
    6. import androidx.room.Room;
    7. import androidx.room.RoomDatabase;
    8. // 与表映射的实体、数据库版本
    9. @Database(entities = {SignRecord.class}, version = 1)
    10. public abstract class AppDatabase extends RoomDatabase {
    11. private static AppDatabase instance;
    12. public static void initial(Context context) {
    13. if (instance == null) {
    14. synchronized (AppDatabase.class) {
    15. if (instance == null) {
    16. // 创建数据库名称和实例
    17. instance = Room.databaseBuilder(context,
    18. AppDatabase.class, "database-name").build();
    19. }
    20. }
    21. }
    22. }
    23. public static AppDatabase getInstance() {
    24. return instance;
    25. }
    26. public abstract BasicDataDao basicDataDao();
    27. }

    BasicDataDao.java文件(数据库表的CRUD等):

    1. package com.example.testgps.database.dao;
    2. import com.example.testgps.bean.SignRecord;
    3. import java.util.List;
    4. import androidx.room.Dao;
    5. import androidx.room.Delete;
    6. import androidx.room.Insert;
    7. import androidx.room.OnConflictStrategy;
    8. import androidx.room.Query;
    9. @Dao
    10. public interface BasicDataDao {
    11. /**
    12. * 打卡记录表操作
    13. */
    14. @Query("SELECT * FROM basic_sign_record")
    15. List getSignRecordList();
    16. @Query("SELECT * FROM basic_sign_record WHERE sign_year_month = :sign_year_month")
    17. List getSignRecordList(String sign_year_month);
    18. @Insert(onConflict = OnConflictStrategy.REPLACE)
    19. void insertSignRecord(SignRecord... item);
    20. @Query("DELETE FROM basic_sign_record")
    21. void deleteSignRecord();
    22. @Query("SELECT COUNT(*) FROM basic_sign_record")
    23. int getSignRecordCount();
    24. @Query("SELECT * FROM basic_sign_record")
    25. List getAll();
    26. @Query("SELECT * FROM basic_sign_record WHERE uid IN (:userIds)")
    27. List loadAllByIds(int[] userIds);
    28. // @Query("SELECT * FROM basic_sign_record WHERE first_name LIKE :first AND " +
    29. // "last_name LIKE :last LIMIT 1")
    30. // SignRecord findByName(String first, String last);
    31. @Insert
    32. void insertAll(SignRecord... signRecordList);
    33. @Insert
    34. void insert(SignRecord signRecord);
    35. @Delete
    36. void delete(SignRecord signRecord);
    37. /**
    38. * 其他表操作
    39. */
    40. }

    SignRecord.java文件(实体):

    1. package com.example.testgps.bean;
    2. import androidx.annotation.NonNull;
    3. import androidx.room.ColumnInfo;
    4. import androidx.room.Entity;
    5. import androidx.room.PrimaryKey;
    6. @Entity(tableName = "basic_sign_record")
    7. public class SignRecord {
    8. @PrimaryKey
    9. @NonNull
    10. @ColumnInfo(name = "sign_date")
    11. public long signDate;
    12. @ColumnInfo(name = "sign_year_month")
    13. public String signYearMonth;
    14. @ColumnInfo(name = "uid")
    15. public long uid;
    16. @ColumnInfo(name = "name")
    17. public String name;
    18. @ColumnInfo(name = "sign_in_time")
    19. public long signInTime;
    20. @ColumnInfo(name = "sign_out_time")
    21. public long signOutTime;
    22. public long getSignDate() {
    23. return signDate;
    24. }
    25. public void setSignDate(long signDate) {
    26. this.signDate = signDate;
    27. }
    28. public String getSignYearMonth() {
    29. return signYearMonth;
    30. }
    31. public void setSignYearMonth(String signYearMonth) {
    32. this.signYearMonth = signYearMonth;
    33. }
    34. public long getUid() {
    35. return uid;
    36. }
    37. public void setUid(long uid) {
    38. this.uid = uid;
    39. }
    40. public String getName() {
    41. return name;
    42. }
    43. public void setName(String name) {
    44. this.name = name;
    45. }
    46. public long getSignInTime() {
    47. return signInTime;
    48. }
    49. public void setSignInTime(long signInTime) {
    50. this.signInTime = signInTime;
    51. }
    52. public long getSignOutTime() {
    53. return signOutTime;
    54. }
    55. public void setSignOutTime(long signOutTime) {
    56. this.signOutTime = signOutTime;
    57. }
    58. }

    在自定义的Application.java文件中去初始化数据库:

    1. public class MyApplication extends Application {
    2. public static MyApplication myApplication;
    3. @Override
    4. public void onCreate() {
    5. super.onCreate();
    6. myApplication = this;
    7. AppDatabase.initial(this);
    8. }
    9. }

    业务代码使用示例,其他的CRUD也是一样的操作:

    1. new Thread(new Runnable() {
    2. @Override
    3. public void run() {
    4. SignRecord signRecord = new SignRecord();
    5. signRecord.setName("小张");
    6. BasicDataDao dataDao = AppDatabase.getInstance().basicDataDao();
    7. dataDao.insertSignRecord(signRecord);
    8. }
    9. }).start();

    到这里就初步实现了Room的初步使用;

    注意:数据库是I/O操作,所以必须执行在子线程中;

    好了,又可以愉快玩耍了。

  • 相关阅读:
    探讨基于IEC61499 的分布式 ISA Batch 控制系统
    【云原生 | 38】Docker快速部署开源脚本语言PHP
    P4学习——Basic Tunneling
    ubuntu 20.04 + Anaconda + cuda-11.8 + opencv-4.8.0(cuda)
    c++二叉树遍历
    【Stable Diffusion系列】(一):AI绘画本地部署教程
    学习STM32第二十天
    Python中记住过去(模型状态)的五种方法
    【UDS 14229-1诊断服务内容详细解读】
    POI-TL制作word
  • 原文地址:https://blog.csdn.net/msn465780/article/details/127728404