现在比较流行google官方的JetPack(喷气背包),这是个什么东东呢?
其实就是谷歌官方的一套工具包,提供各种工具类,因此可以部分不用第三方的开源库了,
比如:Room,Room是安卓设备SqlLite的框架,以前也用过不少类似的第三方开源框架,比如:
GreenDao、Realm;
Realm不推荐使用,真难用,谁用谁知道!
GreenDao还不错!
既然Room是大势所趋,那么学一学它该如何使用吧;
Room 持久性库 | Android 开发者 | Android Developers
目前最高版本是2.4.3,如果用这个版本的话,compileSdk最低要求31,这里就暂时不用最高版本,以后再用;
主module中build.gradle文件,加上依赖:
- implementation 'androidx.room:room-runtime:2.1.0-alpha03'
- annotationProcessor 'androidx.room:room-compiler:2.1.0-alpha03'
- implementation 'androidx.room:room-rxjava2:2.1.0-alpha03'
数据库包结构如下:
AppDatabase.java文件(创建数据库名称、实例等):
- package com.example.testgps.database;
-
- import android.content.Context;
- import com.example.testgps.bean.SignRecord;
- import com.example.testgps.database.dao.BasicDataDao;
- import androidx.room.Database;
- import androidx.room.Room;
- import androidx.room.RoomDatabase;
-
- // 与表映射的实体、数据库版本
- @Database(entities = {SignRecord.class}, version = 1)
- public abstract class AppDatabase extends RoomDatabase {
-
- private static AppDatabase instance;
-
- public static void initial(Context context) {
- if (instance == null) {
- synchronized (AppDatabase.class) {
- if (instance == null) {
- // 创建数据库名称和实例
- instance = Room.databaseBuilder(context,
- AppDatabase.class, "database-name").build();
- }
- }
- }
- }
-
- public static AppDatabase getInstance() {
- return instance;
- }
-
- public abstract BasicDataDao basicDataDao();
-
-
- }
BasicDataDao.java文件(数据库表的CRUD等):
- package com.example.testgps.database.dao;
-
- import com.example.testgps.bean.SignRecord;
- import java.util.List;
- import androidx.room.Dao;
- import androidx.room.Delete;
- import androidx.room.Insert;
- import androidx.room.OnConflictStrategy;
- import androidx.room.Query;
-
- @Dao
- public interface BasicDataDao {
- /**
- * 打卡记录表操作
- */
- @Query("SELECT * FROM basic_sign_record")
- List
getSignRecordList(); -
- @Query("SELECT * FROM basic_sign_record WHERE sign_year_month = :sign_year_month")
- List
getSignRecordList(String sign_year_month); -
- @Insert(onConflict = OnConflictStrategy.REPLACE)
- void insertSignRecord(SignRecord... item);
-
- @Query("DELETE FROM basic_sign_record")
- void deleteSignRecord();
-
- @Query("SELECT COUNT(*) FROM basic_sign_record")
- int getSignRecordCount();
-
- @Query("SELECT * FROM basic_sign_record")
- List
getAll(); -
- @Query("SELECT * FROM basic_sign_record WHERE uid IN (:userIds)")
- List
loadAllByIds(int[] userIds); -
- // @Query("SELECT * FROM basic_sign_record WHERE first_name LIKE :first AND " +
- // "last_name LIKE :last LIMIT 1")
- // SignRecord findByName(String first, String last);
-
- @Insert
- void insertAll(SignRecord... signRecordList);
-
- @Insert
- void insert(SignRecord signRecord);
-
- @Delete
- void delete(SignRecord signRecord);
-
- /**
- * 其他表操作
- */
-
- }
-
SignRecord.java文件(实体):
- package com.example.testgps.bean;
-
- import androidx.annotation.NonNull;
- import androidx.room.ColumnInfo;
- import androidx.room.Entity;
- import androidx.room.PrimaryKey;
-
- @Entity(tableName = "basic_sign_record")
- public class SignRecord {
- @PrimaryKey
- @NonNull
- @ColumnInfo(name = "sign_date")
- public long signDate;
-
- @ColumnInfo(name = "sign_year_month")
- public String signYearMonth;
-
- @ColumnInfo(name = "uid")
- public long uid;
-
- @ColumnInfo(name = "name")
- public String name;
-
- @ColumnInfo(name = "sign_in_time")
- public long signInTime;
-
- @ColumnInfo(name = "sign_out_time")
- public long signOutTime;
-
- public long getSignDate() {
- return signDate;
- }
-
- public void setSignDate(long signDate) {
- this.signDate = signDate;
- }
-
- public String getSignYearMonth() {
- return signYearMonth;
- }
-
- public void setSignYearMonth(String signYearMonth) {
- this.signYearMonth = signYearMonth;
- }
-
- public long getUid() {
- return uid;
- }
-
- public void setUid(long uid) {
- this.uid = uid;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public long getSignInTime() {
- return signInTime;
- }
-
- public void setSignInTime(long signInTime) {
- this.signInTime = signInTime;
- }
-
- public long getSignOutTime() {
- return signOutTime;
- }
-
- public void setSignOutTime(long signOutTime) {
- this.signOutTime = signOutTime;
- }
- }
-
在自定义的Application.java文件中去初始化数据库:
- public class MyApplication extends Application {
-
- public static MyApplication myApplication;
-
- @Override
- public void onCreate() {
- super.onCreate();
- myApplication = this;
- AppDatabase.initial(this);
- }
-
- }
业务代码使用示例,其他的CRUD也是一样的操作:
- new Thread(new Runnable() {
- @Override
- public void run() {
- SignRecord signRecord = new SignRecord();
- signRecord.setName("小张");
- BasicDataDao dataDao = AppDatabase.getInstance().basicDataDao();
- dataDao.insertSignRecord(signRecord);
- }
- }).start();
到这里就初步实现了Room的初步使用;
注意:数据库是I/O操作,所以必须执行在子线程中;
好了,又可以愉快玩耍了。